我无法弄清楚这个Tkinter错误

时间:2011-08-21 04:20:04

标签: python tkinter

我正在使用Python的Tkinter为我正在开发的项目创建GUI。

当我尝试运行部分代码时,我收到此错误:

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'}

1 个答案:

答案 0 :(得分:1)

好的,所以我第一次错过了这个,但问题实际上是你试图将一个Button添加到列表中:

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

如果您只是将Button包装在括号中,则错误消失(这是有道理的,因为您应该能够添加两个列表):

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

原始尝试

我的猜测:

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

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])))