3

I have run into a very weird Problem with Tkinter entry widgets. When I try to enter something into them they don´t accept my input.

After some PC restarting and Python reinstalling I figured out why this happens: I had a messagebox just before the root.mainloop() in the code. The code looks something like this:

def xyz():
    if not messagebox.askyesno("Title","Some text"):
        exit()
xyz()
root.mainloop()

I found, to resolve the issue you can just manually focus on a different window and then back again. I would like to know if there is some better way to do this? I would like to keep my messagebox, AND dont´t want the unelegant solution of manually changing window focus.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Python does not do any magic, so does not know which window you want switched, so you have to code things yourself (whether is it unelegant or not), i.e. focus_set() whatever you want focused. –  Mar 26 '16 at 17:47
  • I think this is a known bug. I'm guessing you're on Windows, because I think that's the only platform that has this bug. What if you do `root.after_idle(xyz)` instead of calling `xyz()` directly? That gives `mainloop` a chance to start up, but still shows the messagebox as soon as the GUI starts up. – Bryan Oakley Mar 26 '16 at 18:38
  • @BryanOakley Thanks, your solution worked perfectly! I didn´t know about `after_idle()` before. – Sebastian Hietsch Mar 26 '16 at 20:55
  • @CurlyJoe Thanks for you reply! What I didn´t mention in my post is that the `root` window is in focus automatically, yet the entry widget doesn´t work. – Sebastian Hietsch Mar 26 '16 at 20:59

1 Answers1

1

You can fix the code like this:

def xyz():
    if not messagebox.askyesno("Title","Some text"):
        exit()
root.after(10,xyz) #show the messagebox after root.mainloop()
root.mainloop()
Lev Leontev
  • 2,538
  • 2
  • 19
  • 31