简体   繁体   English

我不知道这个Tkinter错误

[英]I can't figure out this Tkinter error

I'm using Python's Tkinter to create a GUI for a project i'm working on. 我正在使用Python的Tkinter为我正在处理的项目创建GUI。

When I try to run part of the code though, I get this error: 当我尝试运行部分代码时,出现此错误:

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

The problem is that I can't find the file that the error message references; 问题是我找不到错误消息引用的文件。 my python2.7/lib-tk folder only contains complied versions (.pyo and .pyc) of Tkinter. 我的python2.7/lib-tk文件夹仅包含Tkinter的编译版本(.pyo和.pyc)。

Is there a way to figure out what's going wrong? 有没有办法找出问题所在?

Here's the source of calculator.py 这是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 is a function for evaluating mathematical expressions. full_eval是用于评估数学表达式的函数。

special_characters is a dict containing special characters and their explanations. special_characters是包含特殊字符及其说明的字典。 For now it's just special_characters = {'imaginary unit' : u'\ⅈ'} 现在只不过是special_characters = {'imaginary unit' : u'\ⅈ'}

Ok, so I missed this the first time, but the issue is actually that you are trying to add a Button to a list: 好的,所以我第一次错过了这个,但是问题实际上是您正在尝试将Button添加到列表中:

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

If you simply wrap the Button in brackets, the error goes away (which makes sense because you are supposed to be able to add two lists): 如果仅将Button括在方括号中,那么错误就会消失(这很有意义,因为您应该能够添加两个列表):

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

ORIGINAL ATTEMPT 原始尝试

My guess: 我猜:

special_characters is a dictionary. special_characters是一本字典。 It has key-value mappings where the values are int s. 它具有键值映射,其中值是int Then, when used in self.text_entry.insert(INSERT, char) , text_entry is trying to insert an int into a str and causing the above error. 然后,当在self.text_entry.insert(INSERT, char) ,text_entry试图将一个int插入str中并导致上述错误。 The simple solution: wrap char with str in add_char . 简单的解决方案:在add_char中用str包装char

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

Your other option is to wrap str around the special_characters lookup: 您的另一个选择是将str包裹在special_characters查找周围:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM