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://i.sstatic.net/KRJS8.jpg
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