This is my very easy code:
def test():
if window.winfo_exists():
pass
else:
window = Toplevel(root)
root = Tk()
button = Button(root, text="Test", command=test)
button.pack()
root.mainloop()
I basically just want to check if the window the button opens is already open. If this is the case, the button will do nothing. The problem with my code is that I get a "UnboundLocalError: local variable 'window' referenced before assignment" error when I check the window.
I already searched for hours for a suitable solution. Here is the idea that did not work:
Creating a seperate boolean and set to True, if window is open
- The Problem here is that when I close the window, I wont be able to set the boolean back to False. So I just wont be able to open the window again, when I closed it earlier.
Does anybody know a suitable solution for my problem? Thank you in advance :)