这个问题是Trapping Frame Entrance in tkinter的后续问题,其中@jasonharper提供了非常有用的答案。
在下面的代码中,我将入口绑定到画布(self.c),以便将某些字段从显示模式切换到编辑模式。这似乎适用于textarea,但不适用于menuarea:我可以点击按钮显示menuarea按钮,但当鼠标移动到实际菜单时,画布上的“Leave”事件被触发,所以我无法从菜单中做出选择。为原帖提供的解决方案似乎不适用于此?
from tkinter import *
from tkinter import ttk
class MainWindow(Frame):
def __init__(self,master):
super().__init__(master)
self.master = master
self.pack()
self.edit = 0
self.initUI()
def initUI(self):
self.c = Canvas(self, height = 100, width = 400,bg = "white")
self.c.pack()
self.c.bind('<Enter>', lambda *args: self.trigger(1))
self.c.bind('<Leave>', lambda *args: self.trigger(0))
self.textstring = StringVar()
self.textstring.set("Day")
self.textarea = TextArea(self.c,self.edit,self.textstring)
self.textarea.display(2)
self.menuarea = MenuArea(self.c,self.edit,self.textstring)
self.menuarea.display(2)
self.c.create_window(10,10,window = self.textarea,anchor = NW)
self.c.create_window(200,10, window = self.menuarea, anchor = NW)
def trigger(self,x):
if x == 0:
self.textarea.display(0)
self.menuarea.display(0)
elif x == 1:
self.textarea.display(1)
self.menuarea.display(1)
class TextArea(Frame):
def __init__(self,master,active,textstr):
super().__init__(master)
self.master = master
self.active = active
self.textstr = textstr
self.optionslist = ["GS","NR","JY"]
def display(self,e):
if e == 0:
for child in self.winfo_children():
child.destroy()
L = Label(self,text = self.textstr.get())
L.pack()
elif e ==1:
for child in self.winfo_children():
child.destroy()
E = Entry(self,textvariable = self.textstr)
E.pack(anchor = S)
elif e == 2:
L = Label(self, text=self.textstr.get(), relief="flat")
L.pack()
class MenuArea(Frame):
def __init__(self,master,active,textstr):
super().__init__(master)
self.master = master
self.textstr = textstr
self.optionslist = ["GS", "NR", "JY"]
def display(self,e):
if e == 0:
for child in self.winfo_children():
child.destroy()
L = Label(self,text = self.textstr.get())
L.pack()
elif e ==1:
for child in self.winfo_children():
child.destroy()
E = OptionMenu(self,self.textstr,"Tu", "Wed")
E.config(relief = "flat", width = 3)
E.pack()
elif e == 2:
L = Label(self, text=self.textstr.get())
L.pack()
root = Tk()
mainframe = MainWindow(root)
mainframe.pack()
root.mainloop()
答案 0 :(得分:1)
我猜您正在监控<Leave>
画布的self.c
事件,因为您打算在最终应用中设置多个画面/画布。
在这种情况下,您可以通过将窗口小部件重新排列功能绑定到每个框架/画布的<Enter>
事件来实现您想要的效果:
# High-level parts of the GUI
self.c = Canvas(self, height=100, width=400, bg="white")
self.c.pack()
self.c2 = Canvas(self, height=100, width=400, bg="yellow")
self.c2.pack()
# Re-arrange the widgets when entering different parts of the GUI
self.c.bind('<Enter>', lambda *args: self.trigger(1))
self.c2.bind('<Enter>', lambda *args: self.trigger(0))