gpt4 book ai didi

python - 无模式对话框 tkinter

转载 作者:太空狗 更新时间:2023-10-30 02:49:33 27 4
gpt4 key购买 nike

我正在使用一个名为 customDialog 的类,基于 http://effbot.org/tkinterbook/tkinter-dialog-windows.htm标准代码创建一个子类 Toplevel 的类,并创建模态对话框。我正在尝试调整代码,以便它也可以创建无模式对话框。标准代码调用 Toplevel 构造函数,创建对话框,然后在最后执行 self.wait_window(self),等待对话框窗口关闭。但我没有成功。

我天真的尝试是简单地跳过 wait_window()(这是我在下面截取的代码中的尝试)。我已经尝试了一些其他的东西,但对话框仍然是模态的,也就是说,我无法与应用程序的主窗口进行交互。是什么迫使它成为模态的?这些对话框通常包含小部件和一些按钮。

class Dialog(Toplevel):

def __init__(self, parent, title = None,modal=True):
Toplevel.__init__(self, parent)
self.transient(parent)
if title:
self.title(title)
self.parent = parent
self.result = None
body = Frame(self)
#register validators
self.validatePosInt = (body.register(self.OnValidatePosInt),
'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')

self.initial_focus = self.body(body) #this calls the body function which is overridden, and which draws the dialog
body.grid()
self.buttonbox()
self.grab_set()
if not self.initial_focus:
self.initial_focus = self
self.protocol("WM_DELETE_WINDOW", self.cancel)
self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
parent.winfo_rooty()+50))
self.initial_focus.focus_set()

if modal:
self.wait_window(self)

最佳答案

删除显示 self.grab_set() 的行。 “抓取”意味着抓取所有事件并将它们定向到您不想要的这个窗口。

如果您更深入地阅读获取代码的网页,您会发现它确实提到了这一行及其存在的原因:

... Next, the constructor creates the dialog body, and then calls grab_set to make the dialog modal

关于python - 无模式对话框 tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7687090/

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