Search code examples
pythontkinterdestroytoplevel

Python Tkinter destroy toplevel window missing argument


My Code opens a window with a button. When the button gets clicked a toplevel window is created and the root window gets destroyed. When the button on the toplevel window gets clicked a messagebox opens. I want the the Toplevel Window to be closed when the user presses the ok button of the messagebox. Pressing Ok causes the TypeError: destroy() missing 1 required positional argument: 'self'

I really don't understand why it dosn't work since the toplevel window gets passed as an argument to the destroy method.

import tkinter as tk
from tkinter import messagebox


def main():

    root = tk.Tk()
    root.title("Hauptmenü")

    mainmenue(root)

    root.mainloop()


def mainmenue(root):
    button_rennen = tk.Button(root, text="New Window", width=20,
                              command=lambda: call_window(root))
    button_rennen.pack()


def call_window(root):
    root.destroy()
    rframe = tk.Toplevel

    button = tk.Button(text="Wette platzieren",
                            command=lambda: question(rframe))

    button.pack()


def question(rframe):
    dialog = tk.messagebox.askokcancel(message="Destroy Window?")


    if dialog is True:
        rframe.destroy()


main()

Solution

  • def call_window(root):
        root.destroy()
        rframe = tk.Tk()
    
        button = tk.Button(rframe, text="Wette platzieren",
                                command=lambda: question(rframe))
    
        button.pack()
    

    replace toplevel with Tk window and it works fine.