开发者

I can't figure out this Tkinter error

开发者 https://www.devze.com 2023-03-29 08:10 出处:网络
I\'m using Python\'s Tkinter to create a GUI for a project i\'m working on. When I try to run part of the code though, I get this error:

I'm using Python's Tkinter to create a GUI for a project i'm working on.

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.

Is there a way to figure out what's going wrong?

Here's the source of 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[cha开发者_StackOverflowr]))
        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.

special_characters is a dict containing special characters and their explanations. For now it's just special_characters = {'imaginary unit' : u'\u2148'}


Ok, so I missed this the first time, but the issue is actually that you are trying to add a Button to a list:

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

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

ORIGINAL ATTEMPT

My guess:

special_characters is a dictionary. It has key-value mappings where the values are ints. 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. The simple solution: wrap char with str in 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

Your other option is to wrap str around the special_characters lookup:

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

精彩评论

暂无评论...
验证码 换一张
取 消