[英]Python tkinter: How can I ensure only ONE child window is created onclick and not a new window every time the button is clicked?
Currently learning tkinter and have come to a dead end. 目前正在学习tkinter并已陷入僵局。 Each time I click one of the buttons in my GUI (after typing 'username' and 'password' into the log in screen) a new child window is created and appears.
每次单击GUI中的按钮之一(在“登录”屏幕中键入“用户名”和“密码”后),都会创建并出现一个新的子窗口。 As it should.
正如它应该。 What I would now like to do is to ensure that only one window is created and any subsequent clicks do not create yet more windows.
我现在要做的是确保仅创建一个窗口,并且随后的任何单击都不会创建更多窗口。 How could this be done?
怎么办呢?
from tkinter import *
class Main():
def __init__(self, master):
self.master = master
self.master.title("Main Window")
self.button1 = Button(self.master, text="Click Me 1", command = self.Open1)
self.button1.grid(row=0, column=0, sticky=W)
self.button2 = Button(self.master, text="Click Me 2", command = self.Open2)
self.button2.grid(row=0, column=2, sticky=W)
self.button3 = Button(self.master, text="Close", command = self.Close)
self.button3.grid(row=1, column=0, sticky=W)
def Login(self):
login_win = Toplevel(self.master)
login_window = Login(login_win)
login_window.Focus()
#main_window.Hide()
def Open1(self):
second_window = Toplevel(self.master)
window2 = Second(second_window)
def Open2(self):
third_window = Toplevel(self.master)
window3 = Third(third_window)
def Close(self):
self.master.destroy()
#def Hide(self):
#self.master.withdraw()
#def Appear(self):
#self.master.deiconify()
class Second():
def __init__(self, master):
self.master = master
self.master.title("Child 1 of Main")
self.master.geometry("300x300")
self.button4 = Button(self.master, text="Click Me 1", command = self.Open_Child)
self.button4.grid(row=0, column=0, sticky=W)
def Open_Child(self):
second_child_window = Toplevel(self.master)
window4 = Second_Child(second_child_window)
class Third():
def __init__(self, master):
self.master = master
self.master.geometry("300x300")
self.master.title("Child 2 of Main")
class Second_Child():
def __init__(self, master):
self.master = master
self.master.geometry("400x300")
self.master.title("Child of 'Child 1 of Main'")
class Login():
def __init__(self, window):
self.window = window
self.window.title("Current Library")
Label(self.window, text="Log in to use this program:").grid(row=0, column=0, sticky=W)
self.userbox=Entry(self.window, width=20, bg="light green")
self.userbox.grid(row=1, column=0, sticky=W)
self.passbox=Entry(self.window, width=20, bg="light green")
self.passbox.grid(row=2, column=0, sticky=W)
Button(self.window, text="Submit", width=5, command=self.clicked).grid(row=3, column=0, sticky=W)
def Focus(self):
self.window.attributes('-topmost', 1)
self.window.grab_set()
def clicked(self):
username = self.userbox.get()
password = self.passbox.get()
if password == "password" and username == "username":
self.correct = True
self.window.grab_release()
self.window.destroy()
else:
pass
root_window = Tk()
root_window.iconbitmap(default='transparent.ico')
root_window.geometry("200x100")
main_window = Main(root_window)
main_window.Login()
root_window.mainloop()
Inside the functions which are called when the buttons are clicked could I add an IF statement?: IF window object does not exist then instantiate window object ELSE pass. 在单击按钮时调用的函数内部,是否可以添加IF语句?:如果IF窗口对象不存在,则实例化窗口对象ELSE传递。
def Open1(self):
if window2 == False:
second_window = Toplevel(self.master)
window2 = Second(second_window)
else:
pass
If this is the correct logic then how can I check if a window object has already been created because the above doesn't work as I am referencing the object before it is created? 如果这是正确的逻辑,那么如何检查窗口对象是否已经创建,因为以上内容在我创建对象之前引用该对象时不起作用?
Many thanks. 非常感谢。
Initialize the child window variable in Main's __init__
. 在Main的
__init__
初始化子窗口变量。
self.child_window = None
Then you can check whether it exists in Open1
. 然后,您可以检查它是否存在于
Open1
。
def Open1(self):
if not self.child_window:
self.child_window = Second(Toplevel(self.master))
By the way, if you intend for Second to act like its own window, it's a bit awkward that you have to create a Toplevel every time you want to create a Second. 顺便说一句,如果您打算让Second像它自己的窗口一样工作,那么每次创建Second时都必须创建一个Toplevel有点尴尬。 Conventionally, you would make
Second
a subclass of Toplevel
, so it can be created on its own. 按照惯例,您可以将
Second
作为Toplevel
的子类,以便可以单独创建它。 Something like: 就像是:
class Second(Toplevel):
def __init__(self, master, *args, **kargs):
Toplevel.__init__(self, master, *args, **kargs)
#rest of initialization goes here.
#Use `self` everywhere you previously used `self.master`.
Now you could just do: 现在您可以执行以下操作:
def Open1(self):
if not self.child_window:
self.child_window = Second(self.master)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.