当我通过按住Return键反复打开一个对话框时,程序 最终挂起并打开对话框。没有什么是可点击的,也没有键盘 快捷方式有效。如果我通过单击“离开”来取消对窗口的对焦,则单击“返回到” 它,挂断了。你知道吗
当我通过按住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()
目前没有回答
相关问题 更多 >
编程相关推荐