[Tutor] Tkinter GUI crashing problem
myles broomes
mylesbroomes at hotmail.co.uk
Sat Apr 7 00:07:04 CEST 2012
Im working the Tkinter and I'm having a problem with the GUI I made. It crashes whenever I hit the submit button. Heres my code:
#Guess my number 2.0
#The guess my number game but using a GUI
import random
from tkinter import *
class Application(Frame):
"""GUI to hold widgets. """
def __init__(self,master):
super(Application,self).__init__(master)
self.grid()
self.createWidgets()
def createWidgets(self):
"""GUI widgets. """
#Label instructing the user to take a guess
Label(self,text="Take a guess:").grid(row=0,column=0)
#Entry widget where the user makes a guess
self.guess_ent = Entry(self)
self.guess_ent.grid(row=1,column=0)
#Button that updates the text box
self.submit_bttn = Button(self,command=self.update_txt,text="Submit guess")
self.submit_bttn.grid(row=2,column=0)
#Text box to update the user on whether their guess is correct
self.txt_box = Text(self,width=35,height=5,wrap=WORD)
self.txt_box.grid(row=3,column=0)
def update_txt(self):
"""Updates the text box widget. """
#Get user input from the entry widget
number = random.randint(1,100)
message = ""
guess = None
while self.guess_ent.get():
guess = self.guess_ent.get()
if int(guess) > number:
message += "Lower..."
self.txt_box.delete(0.0,END)
self.txt_box.insert(0.0,message)
else:
message += "Higher..."
self.txt_box.delete(0.0,END)
self.txt_box.insert(0.0,message)
if int(guess) == number:
message += "Congrarulations! You guessed correctly! A new number has been generated."
self.txt_box.delete(0.0,END)
self.txt_box.insert(0.0,message)
#main
root = Tk()
root.title("Guess my number")
app = Application(root)
root.mainloop()
Its frustrating because I dont even get an error code. I coded the last function in a seperate program without a GUI and it runs fine so I have no clue what the problem can be. Any help would be much appreciated.
Myles Broomes
More information about the Tutor
mailing list