【发布时间】:2013-10-12 11:12:40
【问题描述】:
我在 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()
行为:
- 如果你关注“打开对话框”
Button并输入RETURN,一切正常 - 如果你用鼠标点击
Button,对话框就会出现,但是 - 当对话框关闭时,“打开对话框”
Button显示在 其沮丧(tkinter.SUNKEN,如果我没记错的话?)状态。 - (有趣的是,当对话框打开时,
Button是 正常显示。 只有在对话框关闭时才会出现沮丧的表情。) - 我试图通过简单地调用来修复问题
button.config(relief=tkinter.RAISED),但事实并非如此 在这种情况下似乎什么都做。
(实际上,我的完整应用程序开始将按钮显示为按下
在它被点击之后,不仅是在对话框再次关闭之后。
我觉得这更合乎逻辑:
simpledialog 本地事件循环抓取所有事件,因为 simpledialog
是模态的;这可能包括按钮上的<ButtonRelease-1> 鼠标事件?)
问题:
- 为什么会发生这种情况?
- 为什么我的完整应用程序中的行为会有所不同?
- 如何避免或修复两者?
【问题讨论】:
标签: python button dialog tkinter simpledialog