Search code examples
pythonpython-3.xtkinterdestroy

Python - What is tkinter's default window reference?


import tkinter.messagebox
a = tkinter.messagebox.askquestion('','hi')

After the 'askquestion' window closes the tkinter window still remains.

I can resolve this by the following:

import tkinter.messagebox
top = tkinter.Tk()
a = tkinter.messagebox.askquestion('','hi')
top.destroy()

This destroys the window.

My question is:

Is there a way to destroy the window without creating a reference to it?

I tried:

import tkinter.messagebox
a = tkinter.messagebox.askquestion('','hi')
tkinter.Tk().destroy()

but that has no effect.


Solution

  • If you destroy the root window, Tkinter try to recreate one when you call askquestion.

    Don't destory the root window. Instead use withdraw.

    import tkinter.messagebox
    tkinter.Tk().withdraw()
    a=tkinter.messagebox.askquestion('','hi')