计算器GUI'问题'

时间:2014-06-19 21:51:14

标签: user-interface

from tkinter import*

window = Tk()
window.title("T Main")

top_row = Frame(window)
top_row.grid(row=0, column=0, columnspan=2, sticky = N)

display = Entry(top_row, width=45, bg = "light green")
display.grid()

def click():
    display.insert(END, "1")

Button(window, text="1", width=5, command=click).grid(row=1, column=0)

num_pad = Frame(window)
num_pad.grid(row=1, column=0, sticky=W)

num_pad_list = [
    '7','8','9',
    '4','5','6',
    '1','2','3',
    '0','.','=',
    ]

r = 0
c = 0
for buttontext in num_pad_list:
    Button(num_pad, text=buttontext, width=5, command=click).grid(row=r, column=c)
    c = c+1
    if c > 2:
        c = 0
        r = r+1


operator = Frame(window)
operator.grid(row=1, column=1, sticky=E)

operator_list = [
    '+','-',
    '*','/',
    '(',')',
    'C',
    ]

r = 0
c = 0
for buttontext in operator_list:
    Button(operator, text=buttontext, width=5, command=click).grid(row=r, column=c)
    c = c+1
    if c > 1:
        c = 0
        r = r+1



def click(key):
      if key=="=":
            try:
                  result = str(eval(display.get()))[0:10]
                  display.insert = (END, "=" + result)
            except:
                  display.insert = (END, "----> ERROR" )


      elif key == "C":
          display.delete(0, END)

      else:
          display.insert(END, key)




r = 0
c = 0
for buttontext in num_pad_list:
    def cmd(x=buttontext):
        click(x)
    Button(num_pad, text=buttontext, width=5, command=cmd).grid(row=r, column=c)
    c = c+1
    if c > 2:
        c = 0
        r = r+1


r = 0
c = 0
for buttontext in operator_list:
    def cmd(x=buttontext):
        click(x)
    Button(operator, text=buttontext, width=5, command=cmd).grid(row=r, column=c)
    c = c+1
    if c > 1:
        c = 0
        r = r+1





window.mainloop()

这是我的计算器GUI,我在运行时不断收到此错误代码 Tkinter回调中的例外

Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
    return self.func(*args)
  File "C:/Users/Mark/Desktop/practise1.py", line 92, in cmd
    click(x)
  File "C:/Users/Mark/Desktop/practise1.py", line 71, in click
    display.insert(END, key)
TypeError: 'tuple' object is not callable

1 个答案:

答案 0 :(得分:0)

try:
    result = str(eval(display.get()))[0:10]
    display.insert = (END, "=" + result)
except:
    display.insert = (END, "----> ERROR" )

在此,您将display.insert设置为(END, "=" + result)这是一个元组

然后您尝试将其称为函数

display.insert(END, key)

由于您试图调用“{1}}”元组

你可能打算这样做

TypeError: 'tuple' object is not callable