当我按住Return键重复打开一个Dialog时,程序 最终挂起对话框打开。什么都不是可点击的,也没有键盘 快捷方式有效。如果我通过单击离开不聚焦窗口,然后单击返回 它,挂起了。
当我按住Shift-Return重复打开CustomDialog时,程序不会挂起。
Dialog
课程在快速重复打开时有时会使程序挂起?CustomDialog
并且只覆盖一行的Dialog
类不会遇到此问题?cat /dev/urandom
。MainWindow.open_dialog()
。下面的代码创建了一个带有两个按钮的Tk窗口:
tkinter.simpledialog.Dialog
。CustomDialog
的实例,该实例继承
来自tkinter.simpledialog.Dialog
,但注释了一行(line 215)
ok()
方法。import tkinter
from tkinter import ttk
from tkinter.simpledialog import Dialog
class MainWindow:
def __init__(self):
self.root = tkinter.Tk()
self.btn_dialog = ttk.Button(
self.root,
text="Open Dialog",
command=self.open_dialog,
)
self.btn_custom_dialog = ttk.Button(
self.root,
text="Open Custom Dialog",
command=self.open_custom_dialog,
)
self.btn_dialog.pack()
self.btn_custom_dialog.pack()
self.root.bind('<Return>', self.open_dialog)
self.root.bind('<Shift-Return>', self.open_custom_dialog)
self.root.mainloop()
def open_dialog(self, event=None):
u = Dialog(self.root, 'Dialog')
print('Dialog Opened.')
def open_custom_dialog(self, event=None):
u = CustomDialog(self.root, 'Custom Dialog')
print('Custom Dialog Opened.')
class CustomDialog(Dialog):
"""This class is identical to is identical to
tkinter.simpledialog.Dialog, but with
'self.withdraw()' commented out in the self.ok()
method.
"""
def __init__(self, parent, title=None):
tkinter.simpledialog.Dialog.__init__(self, parent, title)
def ok(self, event=None):
"""This method is identical to
tkinter.simpledialog.Dialog.ok(),
but with 'self.withdraw()' commented out.
"""
if not self.validate():
self.initial_focus.focus_set() # put focus back
return
# Using self.withdraw() here causes the
# ui to freeze until the window loses and regains
# focus.
#self.withdraw()
self.update_idletasks()
try:
self.apply()
finally:
self.cancel()
if __name__ == '__main__':
MainWindow()