tkinter - Python 3.3 invalid literal for int() with base 10 -


this first question here on stackoverflow. have been learning python time now, don't wrong code. everytime run program, error:

traceback (most recent call last): file "d:\python\guess number gui.py", line 63, in <module> app = application(root) file "d:\python\guess number gui.py", line 14, in __init__ self.create_widgets() file "d:\python\guess number gui.py", line 37, in create_widgets command = self.check_number() file "d:\python\guess number gui.py", line 44, in check_number guess = int(self.guess.get()) valueerror: invalid literal int() base 10: '' 

this code:

from tkinter import * import random  class application(frame):     """ gui application "guess number" game"""      def __init__(self, master):         """initialize frame. """         super(application, self).__init__(master)         self.the_number = random.randint(1,100)         self.grid()         self.create_widgets()      def create_widgets(self):         """create widgets number , show if it's correct"""         # create instruction label         label(self,               text = "i'm thinking of number between 1-100. can guess it?"               ).grid(row = 0, column = 0, columnspan = 2, sticky = w)          # create label , text entry guess         label(self,               text = "guess: "               ).grid(row = 1, column = 0, sticky = w)         self.guess = entry(self)         self.guess.grid(row = 1, column = 1, sticky = w)          # create text field         self.correct_txt = text(self, width = 40, height = 5, wrap = word)         self.correct_txt.grid(row = 3, column = 0, columnspan = 2)                  # create submit button         button(self,                text = "click submit",                command = self.check_number()                ).grid(row = 2, column = 0, sticky = w)       def check_number(self):          # guess of user         guess = int(self.guess.get())          # see if guess correct or wrong         if guess == self.the_number:             text = ("congratulations, guessed it! , took you", tries, "tries!")             self.correct_txt.delete(0.0, end)             self.correct_txt.insert(0.0, text)          elif guess < self.the_number:             text = "higher..."             self.correct_txt.delete(0.0, end)             self.correct_txt.insert(0.0, text)          elif guess > self.the_number:             text = "lower..."             self.correct_txt.delete(0.0, end)             self.correct_txt.insert(0.0, text)  # main root = tk() root.title("guess number") app = application(root) root.mainloop() 

can tell me what's wrong? in advance!

in button, command should refer name of command, not call command. have now, self.check_number() being called when button created. try this:

button(self,        text = "click submit",        command = self.check_number        ).grid(row = 2, column = 0, sticky = w) 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -