简体   繁体   English

Python tkinter:如何确保在单击时仅创建一个子窗口,而不在每次单击按钮时创建一个新窗口?

[英]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.

相关问题 Python Tkinter:在一个 Z05B8C74CBD96FBF2DE4C1A352702FBF4 上显示每个 window - Python Tkinter : Display every window on one window Tkinter 如何在单击按钮时创建新的聊天窗口实例 - Tkinter How to create new instance of chat window whenever the button is clicked 如何在Tkinter窗口中单击按钮并在Tkinter窗口中显示按钮,打印时间? - How to print time the button got clicked in the Tkinter window and display it in Tkinter window? Tkinter - 我如何制作一个按钮可以创建新的 window 并关闭主 window - Tkinter - How can I make a button can create new window and close the main window 第二次单击导航按钮时,如何阻止 Tkinter window 销毁? - How do I stop Tkinter window from destroy when the navigation button is clicked the second time? TKinter:如何设置子窗口的窗口图标(Toplevel) - TKinter: How can I set the window icon of a child window (Toplevel) 单击按钮时如何打开新的子窗口? (wxpython) - How to open up a new child window when a button is clicked? (wxpython) 在Tkinter 8.5和Python 3.3中单击子窗口的红色X时,如何销毁父窗口 - How to destroy a parent window when child window's red X is clicked in Tkinter 8.5 and Python 3.3 Python Tkinter-使用退出按钮关闭子窗口 - Python Tkinter - closing a child window with an exit button 在tkinter中,如何访问在另一个窗口中创建的变量? - In tkinter, how can I access variables created in one window in a different one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM