【发布时间】:2015-08-09 22:47:30
【问题描述】:
我在使用Toplevel windows 时遇到了这种非常奇怪的行为,我有这样的逻辑:
Did user click 'properties button?'
yes? bind <ButtonPress-1> to window that will call properties(mouselocation)
properties(mouselocation):
find location of cursor
create property window at cursor
我遇到的问题是,当我创建多个窗口并尝试关闭一个时,它们全部关闭。我设法创建了一个突出我的问题的最小示例,PropertiesDialog 类正是我正在使用的,减去一些小的更改,因此它可以用于此示例:
from Tkinter import *
class PropertyDialog(Toplevel):
def __init__(self, root, string):
Toplevel.__init__(self)
self.wm_overrideredirect(1)
self.\
geometry('+%d+%d' %
(root.winfo_pointerx(),
root.winfo_pointery()))
try:
self.tk.call('::Tk::unsupported::MacWindowStyle',
'style', self._w,
'help', 'noActivates')
except TclError:
pass
window_frame = Frame(self)
window_frame.pack(side=TOP, fill=BOTH, expand=True)
exit_frame = Frame(window_frame, background='#ffffe0')
exit_frame.pack(side=TOP, fill=X, expand=True)
button = Button(exit_frame, text='x', width=3, command=self.free,
background='#ffffe0', highlightthickness=0, relief=FLAT)
button.pack(side=RIGHT)
text_frame = Frame(window_frame)
text_frame.pack(side=TOP, fill=BOTH, expand=True)
label = Label(text_frame, text=string, justify=LEFT,
background='#ffffe0',
font=('tahoma', '8', 'normal'))
label.pack(ipadx=1)
def free(self):
self.destroy()
def bind():
"""
toggle property window creation mode
"""
root.bind('<ButtonPress-1>', create)
def create(event):
"""
Create actual window upon mouse click
"""
t = PropertyDialog(root, 'help me')
return
root = Tk()
root.geometry('%dx%d' % (300,400))
Button(root, text='create', command=bind).pack()
root.mainloop()
现在,如果您运行此应用程序,请单击create 并单击窗口的随机区域,然后尝试关闭一个。出于一个原因,每一个窗口都会在试图摧毁一个时关闭。我做错了什么来创建这种行为?
【问题讨论】:
-
我在几分钟内发现了问题,但我不知道如何解决。问题不在于其他窗口正在关闭,而在于它们被隐藏在主窗口后面。如果在关闭多个弹出窗口之一后,您只是移动主窗口而不是退出,您将看到所有其他弹出窗口。
-
我试过在主窗口上使用
lower(),在弹出窗口上使用lift()和focus_set(),没有骰子。 -
其实没关系,我知道了,会发布答案。 :)
标签: python python-2.7 tkinter