当我们将鼠标悬停在组合框上时,是否可以突出显示它?像菜单按钮的activebackground?

2024-06-02 18:30:38 发布

您现在位置:Python中文网/ 问答频道 /正文

当我们将鼠标悬停在按钮上时,是否可以突出显示组合框? b、 因为如果菜单按钮是activebackground这样的选项,当鼠标悬停在菜单按钮上时,它可以帮助高亮显示菜单按钮。在

我尝试了一些组合框的选项,但它只是改变了选择或列表框的颜色等,但当鼠标移动到组合框上时无法突出显示它。在

有人能为这个问题提供一些建议或意见吗?在

#!/usr/intel/bin/python2.7

import Tkinter
from Tkinter import *
from Tkinter import Tk, StringVar
import ttk
import tkFont

try:
    import Tkinter as tk
    import Tkinter.ttk as ttk
except ImportError:
    import Tkinter as tk
    import ttk


class Application:

def __init__(self, parent):
    self.parent = parent
    self.combo()

def combo(self):

    MyFontBtn = tkFont.Font(family='courier', size=20, weight=tkFont.BOLD)

    self.box_value = StringVar()
    self.box = ttk.Combobox(self.parent, textvariable=self.box_value, state='readonly', width=39, font=MyFontBtn)
    self.box['values'] = ('Default', 'User Defined', 'Load From SS')
    self.box.set("Hello Click Drop Down")
    self.box['state'] = 'readonly'
    self.box.bind("<<ComboboxSelected>>", self.print_selected_value)
    self.box.option_add('*TCombobox*Listbox.selectBackground', 'gray50')
    self.box.option_add('*TCombobox*Listbox.font', MyFontBtn)

    #self.box.option_add('TCombobox.background', 'gray50')


    style1 = ttk.Style()
    style1.map('TCombobox', selectbackground=[('readonly', 'red')])
    #style1.map('TCombobox', selectforeground=[('readonly', 'blue')])

    style1.map("self.box",
                foreground=[('pressed', 'red'), ('active', 'blue')]
                #background=[('pressed', '!disabled', 'black'), ('active', 'white')]
              )
    self.box.grid(column=0, row=0)


def print_selected_value(self, *args):
    print "Vaue selected is:", self.box.get()

Tags: importselfboxvaluetkinterdefas菜单
1条回答
网友
1楼 · 发布于 2024-06-02 18:30:38

我设法更改了(非)所选选项的foreground颜色。只有selectbackground发生变化。在鼠标悬停时,Combobox设置hover状态。在

  style1.map('TCombobox', selectforeground=[('hover', 'red')], selectbackground=[('hover', 'green')])
  style1.map('TCombobox', foreground=[('hover', 'red')], background=[('hover', 'green')])

编辑:

下面的修改来确定鼠标交互期间控件的状态标志。在

^{pr2}$

相关问题 更多 >