tkinter askstring deleted before its visibility changed
Asked Answered
I

4

4

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

Ichthyosis answered 26/11, 2018 at 11:44 Comment(2)
It would be much better if you include a minimal reproducible example. Right now there is just an error message with no source to reproduce.Hypophysis
There isn't much more there. It is a method that is started with a thread. I can't show more code, because I am not allowed by the company. If it isn't possible to help like this I will delete the question.Ichthyosis
T
8

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()
Testosterone answered 25/6, 2020 at 18:31 Comment(1)
thank you so much, could be a workaround but it work like a charm!Barriebarrientos
O
0

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.

Overboard answered 8/1, 2021 at 9:36 Comment(1)
This would ideally be better as a comment on the other answer, rather than an answer in itself. However, you have to have a certain amount of reputation before you can do that.Esoterica
G
0

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()
Geesey answered 26/6, 2022 at 14:12 Comment(0)
W
0

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.

Wellgrounded answered 3/1, 2023 at 20:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.