Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.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 防止多个顶级打开?_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 防止多个顶级打开?

Python 防止多个顶级打开?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我有一个面向对象的tkinter程序。 我已经初始化了一个变量,将Toplevel()存储为 self.toplevel = None 然后,当我创建实际的Toplevel窗口时,我只需将其分配给变量: self.toplevel = Toplevel() 问题是……当关闭Toplevel()窗口时,该值仍保留在变量self.Toplevel中。如何在关闭窗口后将变量重置为无,以便执行检查: if (self.toplevel == None): self.toplevel = To

我有一个面向对象的tkinter程序。 我已经初始化了一个变量,将
Toplevel()
存储为

self.toplevel = None
然后,当我创建实际的
Toplevel
窗口时,我只需将其分配给变量:

self.toplevel = Toplevel()
问题是……当关闭
Toplevel()
窗口时,该值仍保留在变量
self.Toplevel
中。如何在关闭窗口后将变量重置为无,以便执行检查:

if (self.toplevel == None):
    self.toplevel = Toplevel()
或者是否有其他方法防止多个
Toplevel
窗口打开?

检查此项

toplevel
关闭后,使用回调函数
TopCloses
将值
None
分配给
self.toplevel
。为此,在GUI类中编写一个方法来访问
toplevel
属性,并在回调函数中将其值设置为
None

在你的主要节目中

def TopCloses():
    top.destroy()
    #Call the setTopLevel method and assign the attribute toplevel value None
    guiObject.setTopLevel(None)

top.protocol("WM_DELETE_WINDOW", TopCloses)
root.mainloop()
检查这个

toplevel
关闭后,使用回调函数
TopCloses
将值
None
分配给
self.toplevel
。为此,在GUI类中编写一个方法来访问
toplevel
属性,并在回调函数中将其值设置为
None

在你的主要节目中

def TopCloses():
    top.destroy()
    #Call the setTopLevel method and assign the attribute toplevel value None
    guiObject.setTopLevel(None)

top.protocol("WM_DELETE_WINDOW", TopCloses)
root.mainloop()
检查这个

toplevel
关闭后,使用回调函数
TopCloses
将值
None
分配给
self.toplevel
。为此,在GUI类中编写一个方法来访问
toplevel
属性,并在回调函数中将其值设置为
None

在你的主要节目中

def TopCloses():
    top.destroy()
    #Call the setTopLevel method and assign the attribute toplevel value None
    guiObject.setTopLevel(None)

top.protocol("WM_DELETE_WINDOW", TopCloses)
root.mainloop()
检查这个

toplevel
关闭后,使用回调函数
TopCloses
将值
None
分配给
self.toplevel
。为此,在GUI类中编写一个方法来访问
toplevel
属性,并在回调函数中将其值设置为
None

在你的主要节目中

def TopCloses():
    top.destroy()
    #Call the setTopLevel method and assign the attribute toplevel value None
    guiObject.setTopLevel(None)

top.protocol("WM_DELETE_WINDOW", TopCloses)
root.mainloop()
以下是我的解决方案:

#somewhere in __init__ make
self.window = None
#I took this piece of code from my bigger app and I have a function 
#self.makevariables(), which is called in init, which contains the line above.
def instructions(self):      
    if self.window == None: #here I check whether it exists, if not make it, else give focus to ok button which can close it
        self.window = Toplevel(takefocus = True)
        #some optional options lol
        self.window.geometry("200x200")
        self.window.resizable(0, 0)
        #widgets in the toplevel
        Label(self.window, text = "NOPE").pack() 
        self.window.protocol("WM_DELETE_WINDOW", self.windowclosed) #this overrides the default behavior when you press the X in windows and calls a function
        self.okbutton = Button(self.window, text = "Ok", command = self.windowclosed, padx = 25, pady = 5)
        self.okbutton.pack()
        self.okbutton.focus()
        self.okbutton.bind("<Return>", lambda event = None:self.windowclosed())
    else:
        self.okbutton.focus()  #You dont need to give focus to a widget in the TopLevel, you can give the focus to the TopLevel, depending how you want it
       #self.window.focus() works too    
def windowclosed(self): #function to call when TopLevel is removed
    self.window.destroy()
    self.window = None
#在某个地方
self.window=None
#我从我更大的应用程序中获取了这段代码,我有一个函数
#self.makevariables(),它在init中调用,init包含上面的行。
def说明(自我):
如果self.window==None:#这里我检查它是否存在,如果不存在,则将焦点交给ok按钮,该按钮可以关闭它
self.window=Toplevel(takefocus=True)
#一些可选选项lol
自窗口几何(“200x200”)
self.window.reshable(0,0)
#顶层的小部件
标签(self.window,text=“NOPE”).pack()
self.window.protocol(“WM_DELETE_window”,self.windowclosed)#当您在windows中按X键并调用函数时,这会覆盖默认行为
self.okbutton=Button(self.window,text=“Ok”,command=self.windowclosed,padx=25,pady=5)
self.okbutton.pack()
self.okbutton.focus()
self.okbutton.bind(“,lambda event=None:self.windowclosed())
其他:
self.okbutton.focus()#您不需要将焦点交给顶级中的小部件,您可以根据需要将焦点交给顶级
#self.window.focus()也可以工作
def windowclosed(self):#删除TopLevel时调用的函数
self.window.destroy()
self.window=None
以下是我的解决方案:

#somewhere in __init__ make
self.window = None
#I took this piece of code from my bigger app and I have a function 
#self.makevariables(), which is called in init, which contains the line above.
def instructions(self):      
    if self.window == None: #here I check whether it exists, if not make it, else give focus to ok button which can close it
        self.window = Toplevel(takefocus = True)
        #some optional options lol
        self.window.geometry("200x200")
        self.window.resizable(0, 0)
        #widgets in the toplevel
        Label(self.window, text = "NOPE").pack() 
        self.window.protocol("WM_DELETE_WINDOW", self.windowclosed) #this overrides the default behavior when you press the X in windows and calls a function
        self.okbutton = Button(self.window, text = "Ok", command = self.windowclosed, padx = 25, pady = 5)
        self.okbutton.pack()
        self.okbutton.focus()
        self.okbutton.bind("<Return>", lambda event = None:self.windowclosed())
    else:
        self.okbutton.focus()  #You dont need to give focus to a widget in the TopLevel, you can give the focus to the TopLevel, depending how you want it
       #self.window.focus() works too    
def windowclosed(self): #function to call when TopLevel is removed
    self.window.destroy()
    self.window = None
#在某个地方
self.window=None
#我从我更大的应用程序中获取了这段代码,我有一个函数
#self.makevariables(),它在init中调用,init包含上面的行。
def说明(自我):
如果self.window==None:#这里我检查它是否存在,如果不存在,则将焦点交给ok按钮,该按钮可以关闭它
self.window=Toplevel(takefocus=True)
#一些可选选项lol
自窗口几何(“200x200”)
self.window.reshable(0,0)
#顶层的小部件
标签(self.window,text=“NOPE”).pack()
self.window.protocol(“WM_DELETE_window”,self.windowclosed)#当您在windows中按X键并调用函数时,这会覆盖默认行为
self.okbutton=Button(self.window,text=“Ok”,command=self.windowclosed,padx=25,pady=5)
self.okbutton.pack()
self.okbutton.focus()
self.okbutton.bind(“,lambda event=None:self.windowclosed())
其他:
self.okbutton.focus()#您不需要将焦点交给顶级中的小部件,您可以根据需要将焦点交给顶级
#self.window.focus()也可以工作
def windowclosed(self):#删除TopLevel时调用的函数
self.window.destroy()
self.window=None
以下是我的解决方案:

#somewhere in __init__ make
self.window = None
#I took this piece of code from my bigger app and I have a function 
#self.makevariables(), which is called in init, which contains the line above.
def instructions(self):      
    if self.window == None: #here I check whether it exists, if not make it, else give focus to ok button which can close it
        self.window = Toplevel(takefocus = True)
        #some optional options lol
        self.window.geometry("200x200")
        self.window.resizable(0, 0)
        #widgets in the toplevel
        Label(self.window, text = "NOPE").pack() 
        self.window.protocol("WM_DELETE_WINDOW", self.windowclosed) #this overrides the default behavior when you press the X in windows and calls a function
        self.okbutton = Button(self.window, text = "Ok", command = self.windowclosed, padx = 25, pady = 5)
        self.okbutton.pack()
        self.okbutton.focus()
        self.okbutton.bind("<Return>", lambda event = None:self.windowclosed())
    else:
        self.okbutton.focus()  #You dont need to give focus to a widget in the TopLevel, you can give the focus to the TopLevel, depending how you want it
       #self.window.focus() works too    
def windowclosed(self): #function to call when TopLevel is removed
    self.window.destroy()
    self.window = None
#在某个地方
self.window=None
#我从我更大的应用程序中获取了这段代码,我有一个函数
#self.makevariables(),它在init中调用,init包含上面的行。
def说明(自我):
如果self.window==None:#这里我检查它是否存在,如果不存在,则将焦点交给ok按钮,该按钮可以关闭它
self.window=Toplevel(takefocus=True)
#一些可选选项lol
自窗口几何(“200x200”)
self.window.reshable(0,0)
#顶层的小部件
标签(self.window,text=“NOPE”).pack()
self.window.protocol(“WM_DELETE_window”,self.windowclosed)#当您在windows中按X键并调用函数时,这会覆盖默认行为
self.okbutton=Button(self.window,text=“Ok”,command=self.windowclosed,padx=25,pady=5)
self.okbutton.pack()
self.okbutton.focus()
self.okbutton.bind(“,lambda event=None:self.windowclosed())
其他:
self.okbutton.focus()#您不需要将焦点交给顶级中的小部件,您可以根据需要将焦点交给顶级
#赛尔夫·温多