2014-03-24 225 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