简体   繁体   中英

How to check if it has a class running?

I made this basic code just to send here to see if I could find a solution.

I wanted the code to do the following: when I pressed the button press it calls the other class with the other button and if I press the button again it will check if the class is still showing so as not to add another X button. of my code for you to see the result and follows a code base for readers to understand.

https://imgur.com/a/UAx6xxd

from tkinter import *
class Window:
    def __init__(self,root):
        self.frame = Frame(root)
        self.frame.pack(side = LEFT)
        self.frame1 = Frame(root)
        self.frame1.pack()
        self.But = Button(self.frame,text = 'Press', command = self.ButOk)
        self.But.pack(side = LEFT,anchor = E)

    def ButOk(self):
        self.Aux = False
        self.Aux = not self.Aux
        if self.Aux:
            Window1(root)

class Window1:
    def __init__(self,root):
        self.fram = Frame(root)
        self.fram.pack(side = LEFT)
        self.fram1 = Frame(root)
        self.fram1.pack()

        self.But = Button(self.fram, text = 'X',command = self.close)
        self.But.pack(side =RIGHT,anchor = W)
    def close(self):
        self.Aux1 = False
        self.Aux1 = not self.Aux1
        if self.Aux1:
            self.fram.pack_forget()
root=Tk()
Window(root)
root.mainloop()

I expect a result in which when the user presses the button, it does not generate this "windows".

If I understood your question correctly, you want to ensure that multiple windows are not created from the multiple clicks of the button on your root level window.

Please try to use TopLevel widget when creating multiple windows. Additionally, you can check if a Toplevel widget exists using the following code:

if tkinter.Toplevel.winfo_exists(toplevel_name)==1:
    self.Aux = False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM