gpt4 book ai didi

python - tkinter陷印框架入口(后续)

转载 作者:太空宇宙 更新时间:2023-11-03 15:14:57 25 4
gpt4 key购买 nike

此问题是 Trapping Frame Entrance in tkinter 的后续问题其中@jasonharper 提供了非常有用的答案。

在下面的代码中,我将入口绑定(bind)到 Canvas (self.c),以便将某些字段从显示模式切换到编辑模式。这似乎适用于文本区域,但不适用于菜单区域:我可以单击按钮来显示菜单区域按钮,但是当鼠标移动到实际菜单时,会触发 Canvas 上的“离开”事件,所以我无法从菜单中进行选择。原帖提供的解决方案似乎不适用于此处?

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()

最佳答案

我猜你正在监视<Leave>您的事件self.c Canvas ,因为您计划在最终应用程序中拥有多个框架/ Canvas 。

在这种情况下,您可以通过将小部件重新排列功能绑定(bind)到 <Enter> 来实现您想要的效果。每个帧/ Canvas 的事件:

# 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))

关于python - tkinter陷印框架入口(后续),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43943767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com