'tkinter messagebox prevents Entry widget from working
I have a program that has a message box to instruct the user. But after I close the message box, I can't enter in the entry widget. Has anyone got an idea of what could be the problem?
My code:
from tkinter import *
import random
import tkinter.messagebox
class Application(Frame):
""" GUI aplication hoger of lager"""
def __init__(self,master):
"""Instaleerd het Frame"""
super(Application, self).__init__(master)
self.grid()
rarara=0;aantal=0;max_pogingen=10;ikdenkaan=random.randrange(1,100)
self.main()
def welkom(self):
"""Create Widgets voor het spelletje hoger of lager"""
#Verwelkomingslebels en instructielabels
msgb=tkinter.messagebox.showinfo("uitleg",
"Welkom bij het spelletje'hoger of lager'.\nIk heb zojuist aan een getal tussen 1 en 100 gedacht.\nJij mag dat getal raden en je hebt 10 pogingen."
)
def labels(self):
#ik denk aan label
Label(self,
text="Ik denk aan:"
).grid(row=0,column=0,sticky=W)
def welkgetal(self):
rarara=Entry(self)
rarara.grid(row=0, column=1,sticky=W)
rarara.configure(state="normal")
def main(self):
self.welkom()
self.labels()
self.welkgetal()
root=Tk()
root.title("Hoger of lager")
root.geometry("350x280+533+244")
app=Application(root)
root.mainloop()
Solution 1:[1]
It seems that Tk modal window behave in a really weird way for event handling :
[0] Python tkinter: stopping event propagation in text widgets tags
[1]http://tiku.io/questions/3361808/python-tkinter-bind-breaking
The fix :
class Application(Frame):
""" GUI aplication hoger of lager"""
def __init__(self,master):
"""Instaleerd het Frame"""
super(Application, self).__init__(master)
self.grid()
rarara=0;aantal=0;max_pogingen=10;ikdenkaan=random.randrange(1,100)
self.main()
master.after(1, self.welkom)
def main(self):
self.labels()
self.welkgetal()
I don't know why, but it works.
Solution 2:[2]
I have been struggling for few days trying to figure out this exact issue where entry widget stops working after a messagebox is shown to screen and closed. My setup was python 3.7 with tkinter.tcl version 8.6.8. After so many trials I have finally fixed it by updating my python to version 3.10.2 and tkinter.Tcl to version 8.6.12. Now it seems to be well responsive before and after a messagebox is shown. It seems like this is a bug in tkinter for some reason not working well at those versions so don't hesitate to update
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | Youstanzr |
