2014-03-24 218 views
-1

當我嘗試運行代碼它給我這個討厭的錯誤:Tkinter的Python代碼錯誤?

Exception in Tkinter callback 

Traceback (most recent call last): 
    File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__ 
    return self.func(*args) 
    File "E:\Tkinter\count_num_CHALLENGE.py", line 39, in count_num 
    for num in range(start, end): 
TypeError: 'Entry' object cannot be interpreted as an integer 

請幫助我不知道什麼是錯的!我嘗試了int()在入口周圍,但也沒有工作如果你們可以幫助我,這將是偉大的。由於我需要幫助

from tkinter import * 

class Application(Frame): 
    def __init__(self, master): 
     super(Application, self).__init__(master) 
     self.grid() 
     self.create_widgets() 

    def create_widgets(self): 
     # Create the instruction Label 
     Label(self, 
       text = "Enter a starting number then an ending number." 
      ).grid(row = 0, column = 0, sticky = W) 

     # Create the entry box for starting/ending 
     self.starting_num = Entry(self) 
     self.starting_num.grid(row = 2, column = 0, sticky = W) 

     self.ending_num = Entry(self) 
     self.ending_num.grid(row = 3, column = 0, sticky = W) 

     # Create the text box 
     self.result_txt = Text(self, width = 20, height = 10, wrap = WORD) 
     self.result_txt.grid(row = 4, column = 0, columnspan = 1) 

     # Submit button 
     Button(self, 
       text = "Count the numbers", 
       command = self.count_num 
       ).grid(row = 5, column = 0, sticky = W) 

    def count_num(self): 
     start = self.starting_num 
     end = self.ending_num 

     for num in range(start, end): 
      print(num) 

     self.result_txt.delete(0.0, END) 
     self.result_txt.insert(0.0, count_num)  

# Main 
root = Tk() 
root.title("Count the numbers") 
app = Application(root) 
root.mainloop() 

回答

0

在你的代碼,self.starting_num是一款入門Widget實例,但你要使用它,就好像它是一個數,正如錯誤消息告訴你。

我打算猜測你的意圖是使用入口小部件的值,在這種情況下,你需要使用類似start=int(self.starting_num.get())的東西,儘管你需要處理入口是emply或者非入口的情況,數字在裏面。 ending_num