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

时间:2018-07-22 22:39:51

标签: python tkinter tkinter-layout tkinter-menu

a。当我们将鼠标悬停在按钮上方时,是否可以突出显示组合框? 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()

1 个答案:

答案 0 :(得分:0)

我设法更改了(未选择)选项的foreground颜色。仅selectbackground更改。 Combobox将鼠标悬停在hover上。

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

编辑:

在修改之后,确定鼠标交互过程中控件的状态标志。

class Application:

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

  def printState(self):
    print ("State Combo:", self.box.state())
    self.parent.after(1000, self.printState)