I read that one can modify the function that the close button of an Tk-Rootwindow uses. So I tried to implement one of my own but obvsly it's crashing. And I don't quite understand why. It seems to don't like the approach of me creating a toplevel popup and to parse the instance of the rootwindow into it. This is the error I get:
File "C:\Users\marti\Documents\GitHub\Kniffel_GUI\game_window.py", line 77, in make_game_window
rootwindow4.protocol("WM_DELETE_WINDOW", on_close(rootwindow4))
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\marti\AppData\Local\Python\pythoncore-3.14-64\Lib\tkinter\__init__.py", line 2400, in wm_protocol
return self.tk.call(
~~~~~~~~~~~~^
'wm', 'protocol', self._w, name, command)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_tkinter.TclError: bad window path name ".!toplevel"
I tried to use a global instance of the rootwindow and assign 'None' to it, but this is not working, I hoped that in runtime the program would be filled with the real instance but nope. So I kind of need to take the rootwindow-Object as an attribute for the function. This is my code:
def make_game_window():
rootwindow4 = tk.Toplevel()
#rootwindow4.iconbitmap('./assets/Dice_Icon.ico')
rootwindow4.title("Best Kniffel")
rootwindow4.geometry("420x600")
rootwindow4.resizable(False, False)
rootwindow4.protocol("WM_DELETE_WINDOW", on_close(rootwindow4))
....
def on_close(root):
close_popup = tk.Toplevel()
close_popup.title("SCHLIEßEN")
close_popup.geometry("400x150")
close_popup.resizable(False, False)
close_popup.configure(bg="black")
button_yes = tk.Button(
close_popup,
bg="red",
text="Schließen",
font=("Arial", 15),
)
button_yes.place(x=75, y=50)
button_yes.bind("", root.destroy())
'''button_no = tk.Button(
close_popup,
bg="green",
text="Weiterspielen",
font=("Arial", 15),
command=close_popup.withdraw()
)
button_no.place(x=200, y=50)'''
Thank you for your help in advance, I know it seems like a weird way of trying to solve this problem. I felt like it all the time while writing this game but somehow it works, so my approach couldn't be so bad. But now I don't know how to deal with this.