2011-08-21 63 views
0

我正在使用Python的Tkinter为我正在处理的项目创建一个GUI。我找不出这个Tkinter错误

当我尝试虽然运行代码的一部分,我得到这个错误:

Traceback (most recent call last): 
    File "calculator.py", line 59, in <module> 
    app = Application() 
    File "calculator.py", line 28, in __init__ 
    self.create_widgets() 
    File "calculator.py", line 45, in create_widgets 
    self.special_chars.create_button(char, self.add_char_event(special_characters[char])) 
    File "calculator.py", line 20, in create_button 
    self.button_list += Button(self, text = txt, command = fcn) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ 
lib-tk/Tkinter.py", line 1206, in cget 
TypeError: cannot concatenate 'str' and 'int' objects 

的问题是,我无法找到文件的错误消息引用;我的python2.7/lib-tk文件夹只包含Tkinter的兼容版本(.pyo和.pyc)。

有没有办法找出发生了什么问题?

这里的calculator.py源

from Tkinter import * 
from exp import full_eval 
from maths import special_characters 

class special_char_frame(LabelFrame): 
    def __init__(self, master = None, text = 'Special Characters'): 
     LabelFrame.__init__(self, master) 
     self.grid() 
     self.button_list = [] 
    def create_button(self, txt, fcn): 
     self.button_list += Button(self, text = txt, command = fcn) 
     self.button_list[-1].grid(row = 0) 


class Application(Frame): 
    def __init__(self, master = None): 
     Frame.__init__(self, master) 
     self.grid() 
     self.create_widgets() 
    def create_widgets(self): 
     ## equation entry pane 
     self.text_entry = Entry(self, width = 100) 
     self.text_entry.grid(row = 0, column = 0) 
     self.text_entry.bind('<KeyPress-Return>', self.calculate) 
     ## result pane 
     self.result = StringVar() 
     self.result_label = Label(self, textvariable = self.result, wraplength = 815, justify = LEFT) 
     self.result_label.grid(row = 1, column = 0, columnspan = 2, sticky = W) 
     self.result.set('') 
     ## calculate button 
     self.calc_button = Button(self, text = 'Calculate', command = self.calculate) 
     self.calc_button.grid(row = 0, column = 1) 
     ## special character button pane 
     self.special_chars = special_char_frame(self) 
     for char in special_characters: 
      self.special_chars.create_button(char, self.add_char_event(special_characters[char])) 
     self.special_chars.grid(column = 0, columnspan = 2, row = 2) 
    def calculate(self, event = None): 
     try: 
      self.result.set(full_eval(self.text_entry.get())) 
     except Exception as error: 
      raise 
      #self.result.set(str(error)) 
     self.text_entry.select_range(0, END) 
    def add_char_event(self, char): 
     def add_char(self = self, event = None): 
      self.text_entry.insert(INSERT, char) 
     return add_char 

app = Application() 
app.master.title('Calculator') 
app.mainloop() 

full_eval是评估数学表达式的功能。

special_characters是一个字典,其中包含特殊字符及其解释。现在它只是special_characters = {'imaginary unit' : u'\u2148'}

+4

邮政完整回溯。 –

+0

另外,发布剩余的代码,或者至少将代码运行到发生问题的地步。 – agf

+0

@Ignacio Vazquez-Abrams在那里......但我更感兴趣的是学习如何找出错误,而不是学习实际的错误。像引用一个我无法找到的文件的错误消息是怎么处理的? – smackcrane

回答

1

好了,我该错过了第一次,但这个问题实际上是你正尝试将按钮添加到列表:

self.button_list += Button(self, text = txt, command = fcn) 

如果你简单地包裹在按钮托架,该错误消失(这是有道理的,因为你应该能够添加两个列表):

self.button_list += [Button(self, text = txt, command = fcn)] 

ORIGINAL未遂

我的猜测:

special_characters是一本字典。它具有值为int s的键值映射。然后,当在self.text_entry.insert(INSERT, char)中使用时,text_entry试图向int中插入int并导致上述错误。简单的解决方案:在add_char中包装charstr

def add_char_event(self, char): 
    def add_char(self = self, event = None): 
     self.text_entry.insert(INSERT, str(char)) 
    return add_char 

你的另一个选择是环绕special_characters查找STR:

for char in special_characters: 
     self.special_chars.create_button(char, 
      self.add_char_event(str(special_characters[char]))) 
+0

有趣的想法,但我试过,并得到了同样的错误 – smackcrane

+0

当我尝试第二个想法,我得到了一个不同的错误:'self.special_chars.create_button(char,self。add_char_event(str(special_characters [char]))) UnicodeEncodeError:'ascii'编解码器无法在位置0编码字符u'\ u2148':序号不在范围内(128)' – smackcrane

+0

所以我只是改变了'u'u \ 2148''改为正常字符(''i'')来摆脱错误,并且第一个错误(关于连接int和str)再次出现 – smackcrane