tkinter askstring deleted before its visibility changed

2.4k views Asked by At

I am trying to make a popup window where someone can fill in a string in an Entry box. I have gone through many examples, but it doesn't work.

I am trying to this:

    var_entry = simpledialog.askstring("Test", "Test")

I get this error message:

_tkinter.TclError: window ".!_querystring" was deleted before its visibility changed

Thanks in advance!

edit: posted wrong error message

4

There are 4 answers

0
flandi On

I'm not sure without your code example but I figured that this error message means that the variable where you are putting the 'askstring' return value or initialvalue is going out of scope before the dialog window is finished. When I had this error message I placed a declaration of the variables outside the inner scope. Forgive me if I am talking here using C concepts (which most Python users would rather ignore).

answer = "dummy"
query_str = "dummy" # without these lines query_str and answer 
                    # can be cleaned up by Python 
                    # before the 'askstring' is done with them
while (1):
    query_str = "0"
    answer = simpledialog.askstring("Get Number",
                                   "Enter NextNumber",
                                   initialvalue = query_str)
    print(answer)
    print(query_str)
    time.sleep(1)

Adding the declarations outside the loop scope worked for me.

1
高見知英 On

I was also able to work around the problem by using the above workaround suggested by John D.

I did some research on this, and it seems that this exception is raised when all of the following conditions are met.

  • The thread that called the simpledialog.askstring method is not the main thread.
  • The Tk window specified in the parent or the Tk window specified in the default_root variable is different from the thread that called the simpledialog.askstring method.

However, I could not come up with a process to deal with this problem. I hope this helps to solve the problem.

0
Andy Zhang On

This issue is caused by askstring is not called in the main thread, which is the main thread. Make sure to call this method in the main thread. For example, this code works well.

from tkinter import Tk
from tkinter.simpledialog import askstring
a = Tk()
askstring('1', '2')
a.mainloop()

And this code will throw your exception when you close the dialog window.

from tkinter import Tk
from tkinter.simpledialog import askstring
from threading import Thread
a = Tk()
Thread(target=askstring, args=('1', '2')).start()
a.mainloop()
1
John D On

I know this is an old thread, but I'm having the same problem and so far haven't found the root cause.

However, this workaround works for me in case anyone else needs it:

#Create a new temporary "parent"

newWin = Tk()

#But make it invisible

newWin.withdraw()

#Now this works without throwing an exception:

retVal = simpledialog.askstring("Enter Value","Please enter a value",parent=newWin)

#Destroy the temporary "parent"

newWin.destroy()