Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter销毁顶级窗口也会销毁其他窗口_Python_Python 2.7_Tkinter - Fatal编程技术网

Python Tkinter销毁顶级窗口也会销毁其他窗口

Python Tkinter销毁顶级窗口也会销毁其他窗口,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我在Toplevelwindows中遇到了这种非常奇怪的行为,我有这样一些逻辑: 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

我在
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

现在,如果运行此应用程序,请单击
create
并单击窗口的随机区域,然后尝试关闭one。出于一个原因,每个窗口在试图只摧毁一个窗口时都会关闭。创建这种行为我做错了什么?

其他窗口没有被破坏,它们只是被主窗口隐藏。如果创建一些弹出窗口,关闭其中一个,然后移动主窗口,则可以看到它们

要解决此问题,请将主窗口降低到所有其他窗口的下方。更改的区域标记为
#changed#

从Tkinter导入*
类属性对话框(顶级):
定义初始化(self、root、string):
顶层初始化(自)
self.wm_overrideredirect(1)
self.root=root#已更改#保存对根的引用
自我\
几何体('++%d++%d'%
(root.winfo_pointerx(),
root.winfo_pointery())
尝试:
self.tk.call('::tk::unsupported::MacWindowsStyle',
“风格”,自我,
“帮助”,“无激活”)
除错误外:
通过
窗框=窗框(自)
窗框包装(侧边=顶部,填充=两侧,展开=真)
退出帧=帧(窗口帧,背景='#ffffe0')
退出框架包装(侧面=顶部,填充=X,展开=真)
按钮=按钮(退出框架,文本=x',宽度=3,命令=self.free,
背景='#ffffe0',高光厚度=0,浮雕=平坦)
按钮包(侧面=右侧)
文本框架=框架(窗口框架)
text_frame.pack(side=TOP,fill=BOTH,expand=True)
label=label(text\u frame,text=string,justify=LEFT,
背景=“#ffffe0”,
字体=('tahoma','8','normal'))
标签包(ipadx=1)
无def(自我):#改变#此方法显著
self.destroy()#首先我们摧毁这个
对于val,枚举(对话框)中的小部件:#浏览对话框列表
如果小部件为self:#当我们找到此小部件时
对话框。弹出(val)#弹出它
打断#并停止搜索
如果对话框:#如果还剩下任何对话框:
对于对话框中的小部件:#检查每个小部件
widget.lift(高于this=self.root)#并将其提升到根之上
def bind():
"""
切换属性窗口创建模式
"""
root.bind(“”,创建)
def create(event):#更改#将小部件存储在列表中
"""
鼠标单击时创建实际窗口
"""
append(PropertyDialog(root,'help me'))
root=Tk()
对话框=[]已更改以初始化列表
root.geometry(“%dx%d%”(300400))
按钮(root,text='create',command=bind).pack()
root.mainloop()

我在几分钟内发现了这个问题,但我不知道如何解决它。问题不是其他窗口正在关闭,而是它们被隐藏在主窗口后面。如果在关闭几个弹出窗口中的一个后,您只需移动主窗口而不是退出,您将看到所有其他弹出窗口。我已尝试在主窗口上使用
lower()
,在弹出窗口上使用
lift()
focus\u set()
,无需掷骰子。实际上,没关系,我得到了它,并将发布答案。:)
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()
from Tkinter import *

class PropertyDialog(Toplevel):
    def __init__(self, root, string):
        Toplevel.__init__(self)
        self.wm_overrideredirect(1)
        self.root = root #CHANGED# save a reference to the root
        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): #CHANGED# this method significantly
        self.destroy() # first we destroy this one
        for val,widget in enumerate(dialogs): # go through the dialogs list
            if widget is self: # when we find this widget
                dialogs.pop(val) # pop it out
                break # and stop searching
        if dialogs: # if there are any dialogs left:
            for widget in dialogs: # go through each widget
                widget.lift(aboveThis=self.root) # and lift it above the root

def bind():
    """
    toggle property window creation mode
    """
    root.bind('<ButtonPress-1>', create)


def create(event): #CHANGED# this to store the widget in a list
    """
    Create actual window upon mouse click
    """
    dialogs.append(PropertyDialog(root, 'help me'))

root = Tk()
dialogs = [] #CHANGED# to initialize a list
root.geometry('%dx%d' % (300,400))

Button(root, text='create', command=bind).pack()

root.mainloop()