我正在使用Windows 7上的Python 3.3。
我有一个基于tkinter
的应用程序,其中一个Button
会启动一个tkinter.simpledialog.Dialog
。
像这样(这是一个完整可运行的示例):
import tkinter
import tkinter.simpledialog
class Mainframe(tkinter.Frame):
def __init__(self, parent):
super(Mainframe, self).__init__(parent)
self.parent = parent
self.button = tkinter.Button(self, text="Open Dialog")
open_dialog_op = lambda ev: self.open_dialog(ev)
self.button.bind("<Button-1>", open_dialog_op)
self.button.bind("<Return>", open_dialog_op)
self.button.pack(side=tkinter.LEFT)
def open_dialog(self, event):
dialog = tkinter.simpledialog.Dialog(self.parent, "My Dialog")
self.button.config(relief=tkinter.RAISED) # does not help!
root = tkinter.Tk()
Mainframe(root).pack()
root.mainloop()
行为:
- 如果您将焦点放在“打开对话框”按钮上并键入“RETURN”,一切都很好
- 如果您单击鼠标“按钮”,则对话框出现得很好,但是
- 当对话框关闭时,“打开对话框”按钮以其沉没状态(如果我没有错的话,则为tkinter.SUNKEN)显示。
- (有趣的是,在对话框打开时,按钮通常显示。 只有在对话框关闭后才会开始压抑的外观。)
- 我尝试通过简单调用button.config(relief=tkinter.RAISED)来修复问题, 但在这种情况下似乎根本没有任何作用。
(实际上,我的完整应用程序在单击按钮后就开始显示按钮被按下的状态, 而不仅仅是在对话框再次关闭后。 我认为这更加合理: simpledialog本地事件循环抓取所有事件,因为simpledialog是模态的; 这可能包括ButtonRelease-1鼠标事件在按钮上?)
问题:
- 为什么会发生这种情况?
- 为什么我的完整应用程序中的行为可能不同?
- 如何避免或修复两者?
<ButtonRelease>
上添加绑定,而是使用内置的command
属性,它可以正确地处理所有绑定和行为。 - Bryan Oakley