[Tutor] Tkinter GUI crashing problem

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] Tkinter GUI crashing problem

myles broomes

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    
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Tkinter GUI crashing problem

Emile van Sebille
On 4/6/2012 3:07 PM myles broomes said...
>
> import random
> from tkinter import *


What version of python on what platform please...

Emile



_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Tkinter GUI crashing problem

Alan Gauld
In reply to this post by myles broomes
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/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Tkinter GUI crashing problem

Steven D'Aprano-8
In reply to this post by myles broomes
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:

Define "crashes". Does it:

* cause your computer to Blue Screen of Death?

* lock up your computer until you Ctrl-Alt-Delete?

* cause Windows to put up an error message saying the application has
   crashed and would you like to submit diagnostics to Microsoft?

* print a standard Python traceback?

* something else?


If you get a traceback, please COPY AND PASTE the entire traceback into an
email and send that. Do not retype it by hand, summarise, simplify or
otherwise reword it.




--
Steven

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor