2013-05-09 33 views
0

這是我在Stackoverflow上的第一個問題。我一直在學習Python一段時間,但我並沒有真正明白我的代碼有什麼問題。每次我跑我的程序,我得到這個錯誤:Python 3.3無效文字爲int()與基地10

Traceback (most recent call last): 
File "D:\Python\guess the number gui.py", line 63, in <module> 
app = Application(root) 
File "D:\Python\guess the number gui.py", line 14, in __init__ 
self.create_widgets() 
File "D:\Python\guess the number gui.py", line 37, in create_widgets 
command = self.check_number() 
File "D:\Python\guess the number gui.py", line 44, in check_number 
guess = int(self.guess.get()) 
ValueError: invalid literal for int() with base 10: '' 

這是代碼:

from tkinter import * 
import random 

class Application(Frame): 
    """ GUI application for the "Guess my 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 to get the number and show if it's correct""" 
     # create instruction label 
     Label(self, 
       text = "I'm thinking of a number between 1-100. Can you guess it?" 
      ).grid(row = 0, column = 0, columnspan = 2, sticky = W) 

     # create label and text entry for the 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 a text field 
     self.correct_txt = Text(self, width = 40, height = 5, wrap = WORD) 
     self.correct_txt.grid(row = 3, column = 0, columnspan = 2)   

     # create a submit button 
     Button(self, 
       text = "Click to submit", 
       command = self.check_number() 
       ).grid(row = 2, column = 0, sticky = W) 


    def check_number(self): 

     # get the guess of the user 
     guess = int(self.guess.get()) 

     # see if the guess is correct or wrong 
     if guess == self.the_number: 
      text = ("Congratulations, you guessed it! And it only 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 the number") 
app = Application(root) 
root.mainloop() 

有人能告訴我什麼是錯的?提前致謝!

+1

然後你進入'guess'文本框是什麼?這不是一個有效的數字。 – 2013-05-09 12:28:36

+0

顯然你在文本框中輸入了一個空行,它基本上不能轉換爲整數。 – favoretti 2013-05-09 12:29:27

+0

驗證輸入:http://stackoverflow.com/questions/8959815/restricting-the-value-in-tkinter-entry-widget – kalgasnik 2013-05-09 12:36:29

回答

3

Buttoncommand應該指的是命令的名稱,而不是對命令的調用。正如你現在所做的那樣,當按鈕被創建時,self.check_number()被調用。試試這個:

Button(self, 
     text = "Click to submit", 
     command = self.check_number 
     ).grid(row = 2, column = 0, sticky = W) 
+0

感謝這工作! – LexGr 2013-05-10 08:29:08

相關問題