【问题标题】:TypeError for indexing list with float values in PythonPython中带有浮点值的索引列表的TypeError
【发布时间】:2021-04-08 13:10:30
【问题描述】:

我正在尝试使用 Tkinter 创建一个简单的计算器。为此,我创建了一个列表来存储我想要操作的数字,以便我可以使用该列表进行加/减/乘/除。但是,当我尝试访问列表时,我收到了 TypeError,即使我已将列表中的值转换为浮点数。这是我不断收到的错误:第 37 行,在计算中

result = operators[current_operator(current_input[0], current_input[1])]
TypeError: 'str' object is not callable

这是我的代码:

import tkinter as tk
from tkinter import messagebox

# take number function
def take_number(event):
    global current_input
    global input_box
    global current_number
    current_number += event.widget["text"]
    input_box.insert(10, current_number)



# take operator event and append current number to number list
def take_operator(event):
    global current_input
    global current_number
    global current_operator
    current_operator = event.widget["text"]
    input_box.insert(10, current_operator)
    current_input.append(float(current_number))
    current_number = ""




# calculate by appending current number to number list and using the list to get first and second number
def calculate():
    global current_input
    global current_operator
    global input_box
    current_input.append(float(current_number))
    input_box.delete(0, 10)
    result = operators[current_operator(current_input[0], current_input[1])]
    input_box.insert(10, result)


# create window
window = tk.Tk()
window.title("Pocket calculator")
current_input = []
current_number = ""
current_operator = ""
# create a dictionary to link to operators
operators = {"+": lambda x, y: x + y,
             "-": lambda x, y: x - y,
             "*": lambda x, y: x * y,
             "/": lambda x, y: x / y}

# create enter box
input_box = tk.Entry(window, text="0", width=10)
input_box.grid(column=0, row=0)

# create buttons
number_0 = tk.Button(window, text="0")
number_0.grid(column=0, row=4)
number_0.bind("<Button-1>", take_number)

for i in range(3):
    one_to_three = tk.Button(window, text=str(i+1))
    one_to_three.grid(column=i, row=3)
    one_to_three.bind("<Button-1>", take_number)

for i in range(3, 6):
    four_to_six = tk.Button(window, text=str(i+1))
    four_to_six.grid(column=i % 3, row=2)
    four_to_six.bind("<Button-1>", take_number)

for i in range(6, 9):
    seven_to_nine = tk.Button(window, text=str(i+1))
    seven_to_nine.grid(column=i % 3, row=1)
    seven_to_nine.bind("<Button-1>", take_number)

c_button = tk.Button(window, text="C")
c_button.grid(column=1, row=4)

dot_button = tk.Button(window, text=".")
dot_button.grid(column=2, row=4)
dot_button.bind("<Button-1>", take_number)

equal_button = tk.Button(window, text="=", command=calculate)
equal_button.grid(column=3, row=3)


sign_button = tk.Button(window, text="+/-")
sign_button.grid(column=3, row=4)

plus_button = tk.Button(window, text="+")
plus_button.grid(column=4, row=1)
plus_button.bind("<Button-1>", take_operator)

minus_button = tk.Button(window, text="-")
minus_button.grid(column=4, row=2)
minus_button.bind("<Button-1>", take_operator)

multiply_button = tk.Button(window, text="*")
multiply_button.grid(column=4, row=3)
multiply_button.bind("<Button-1>", take_operator)

division_button = tk.Button(window, text="/")
division_button.grid(column=4, row=4)
division_button.bind("<Button-1>", take_operator)


# start application
window.mainloop()

【问题讨论】:

  • 列表索引必须是整数。
  • 索引是整数,只是列表中的值是浮点数。

标签: python list tkinter typeerror calculator


【解决方案1】:

operators[current_operator(current_input[0], current_input[1])]

operators 是一个 dictionary,带有 str 键和 lambda 值。

您正在拨打current_operator(..),但您需要拨打operators[current_operator](..)

所以将你的行改为:

operators[current_operator](current_input[0], current_input[1])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 2022-11-17
    • 2023-04-06
    相关资源
    最近更新 更多