-1
我想要做的一个弹出列表:请与Tkinter的
- 让一个按钮。
- 如果将鼠标悬停在上面,会出现顶层,如果删除光标,则顶层消失。
- 如果将光标从按钮移动到此顶层,则不会(!)消失,并且如果将光标移动到其他位置,它最终会消失。
为此,我创建了2个类(按钮和顶层)。我试着结合输入和离开他们两个,但是这会导致两个问题:
- 如果从按键移动光标,顶层应该被摧毁,但它与func_1创建和销毁与func_2导致错误。
- 为了从按钮移动光标到顶级安全我试图删除按钮的离开绑定,然后将其重新添加顶层的离开绑定,但它只会导致更多的问题。
这是我现在有:
import tkinter as tk
class PopupButton(tk.Button):
def __init__(self, *args, **kwargs):
tk.Button.__init__(self, *args, **kwargs)
self.bind("<Enter>", self.button_enter)
self.bind("<Leave>", self.button_leave)
def button_enter(self, event):
self.configure(bg="grey")
toplevel = Popup(root, bg="lightblue")
toplevel.overrideredirect(1)
x = root.winfo_x()
y = root.winfo_y()
toplevel.geometry("100x100+{}+{}".format(x+160, y+485))
def button_leave(self, event):
self.configure(bg="black")
pass
# problem here
class Popup(tk.Toplevel):
def __init__(self, *args, **kwargs):
tk.Toplevel.__init__(self, *args, **kwargs)
self.bind("<Enter>", self.toplevel_enter)
self.bind("<Leave>", self.toplevel_leave)
def toplevel_enter(self, event):
pass
# problem here
def toplevel_leave(self, event):
self.destroy()
root = tk.Tk()
root.configure(bg="white")
root.geometry("400x600")
button = PopupButton(root, text="Hover over me", font="courier 20", bg="black", fg="white")
button.pack(side="bottom")
root.mainloop()
这是我所能做的最多。感谢任何帮助。
如果你至少包含了一点解释你做了什么,那么你的答案会更好。否则,我们不得不阅读代码并逐行比较原始代码以查看更改内容。 –