[Tutor] Tkinter GUI crashing problem

Alan Gauld alan.gauld at btinternet.com
Sat Apr 7 01:33:25 CEST 2012


On 06/04/12 23:07, myles broomes wrote:
>
> 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:

What do you mean by crashes?
It looks to me like it should lock up rather than  crash.
Your update_txt method goes into an infinite loop:

 >                  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)
 >

Since this never exits the screen never refreshes to allow the user to 
enter a new value in the guess_ent box.

loops inside event handling methods are always risky and should be 
avoided if at all possible. Open ended while loops are especially prone 
to non termination. If you need to process something repeatedly its 
usually better in a GUI program to do it via a timer event that 
continually calls the function. (Or using a background thread but thats 
a whole different can of worms)

But in this case the whole while loop is completely redundant. Tkinter 
provides an event loop, you just need to handle one guess at a time and 
let the user press submit each time.

If you really are crashing please run the code from inside an OS console 
and capture the stack trace and send it to us.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list