【问题标题】:tkinter, print text when hovering over OptionMenu objectstkinter,将鼠标悬停在 OptionMenu 对象上时打印文本
【发布时间】:2018-12-14 06:53:11
【问题描述】:

所以我必须更改颜色以及更改颜色,同时将鼠标悬停在使用 tkinter 中的 OptionMenu 创建的菜单对象上。

当我将鼠标悬停在按钮上时,我什至可以打印文本,但是当我第二次下拉菜单时,它就不再打印了。

我做错了什么?当您单击以打开 OptionMenu 并在选择中四处移动时,我如何打印?

from tkinter import *
import tkinter as tk
OZoneIzotopeSemiWhite = "#c0c4ca"
buttonBackground = "#303336"
buttonforeground = "#cdd0d7"
BACKGROUND2 = "#1e1f21"

class DropDownButton():
    def __init__(self, parent, placement, opTions, **kw):
        self.parent = parent
        self.options = opTions

        self.om_variable = tk.StringVar(self.parent)
        self.om_variable.set(self.options[0])
        self.om_variable.trace('w', self.option_select)

        self.om = tk.OptionMenu(self.parent, self.om_variable, *self.options)
        self.om["menu"].config(fg=buttonforeground, bg=buttonBackground, activebackground=OZoneIzotopeSemiWhite, activeforeground=BACKGROUND2, borderwidth = 0)
        self.om.config(fg=buttonforeground, bg=buttonBackground, activebackground=OZoneIzotopeSemiWhite, activeforeground=BACKGROUND2, bd =0)
        self.om.place(x = placement, y = 2)
        self.om.bind("<Enter>", self.on_enter)
        self.om.bind("<Leave>", self.on_leave)

    def on_enter(self, event):
        if self.om == self.options[0]:
            print ("Hello")
        elif self.om_variable.get() == self.options[1]:
            print ("Hello 2!")
        else:
            print("Hell0 3!")

    def on_leave(self, enter):
        print ("leave")

    def option_select(self, *args):
        print (self.om_variable.get())

root = tk.Tk()
DropDownButton(root, 55, ['one', 'two', 'three'])
root.mainloop()

【问题讨论】:

    标签: python-3.x tkinter tk optionmenu tkinter-menu


    【解决方案1】:

    您没有做错任何事情,但您可能需要改变您所做的事情或您的期望。当通过单击菜单按钮小部件(由选项菜单代码创建)弹出菜单时,鼠标指针被弹出的菜单抓住,并且这种状态一直持续到您执行操作选择某些东西(或者您在菜单外单击或释放,这会取消)。您的样式(取决于菜单按钮的状态)可能会注意到这一点,并会做出一些让您有些意想不到的事情。

    顺便说一句,如果您使用键绑定弹出菜单,不同的外观实际上很有用,因为焦点也会在实际的弹出菜单上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-06
      • 2015-08-06
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多