【问题标题】:TypeError: 'str' object is not callable 1TypeError: 'str' 对象不可调用 1
【发布时间】:2019-06-11 20:38:46
【问题描述】:

我想解决问题或使用其他方法输入此命令行

from tkinter import *
from tkinter import ttk
from tkinter import messagebox

root = Tk()
ent = ttk.Entry(root, width=40)
ent.pack()

ent1 = ttk.Entry(root, width=40)
ent1.pack()

bu = ttk.Button(root, text="click")
bu.pack()

i = ent.get()

sentence = i
sentence.replace(" ", "")


def buclick():
    if i == i():
        i()
    else:
        messagebox.showinfo("Error", "There is no product with this name")


bu.config(command=buclick)

print("Product" + "      " + "quantity" + "      " + "Price per one" + "      " + "Total")


class Product:
    index = 0
    item = ''
    quantity = 0
    price_per_once = 0
    total = 0

    def __init__(self, item, quantity, price_per_once, total):
        self.item = item
        self.quantity = quantity
        self.price_per_once = price_per_once
        self.total = total

    def return_information(self):
        return self.item + "          " + str(self.quantity) + "               " + str(
            self.price_per_once) + "             " + str(self.sum)

    def print_information(self):
        print(self.return_information())


def ic5501():
    item = "ic 550"
    result = int(ent1.get())
    quantity = result
    price_per_once = 15
    total = (15 * result)
    ic550 = Product(item, quantity, price_per_once, total)
    ic550.return_information()
    ic550.print_information()


def ic5502():
    item = "ic 5502"
    result = int(ent1.get())
    quantity = result
    price_per_once = 20
    total = (20 * result)
    ic550 = Product(item, quantity, price_per_once, total)
    ic550.return_information()
    ic550.print_information()


root.mainloop()


Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Ahmed Rabea Smaha\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
    return self.func(*args)
  File "C:/Users/Ahmed Rabea Smaha/PycharmProjects/untitled/untitled.py", line 22, in buclick
    if i == i():
TypeError: 'str' object is not callable

【问题讨论】:

  • i 是一个字符串,当您尝试像i() 这样调用该字符串时,您会做什么?
  • 我怀疑if i == i(): i() 的预期行为是“如果字符串i 与该程序中的函数同名,则调用该函数”。不幸的是,这样做比您当前的方法更复杂。
  • 是的,我想要@Kevin 我怎么能用另一种简单的方法做到这一点

标签: python python-3.x


【解决方案1】:

根据您提供给我们的信息,您似乎在尝试if i == i() 的第22 行尝试检查变量i 是否对应于函数i() 的返回值。在我看来,您在任何地方都没有函数 i() (无论如何,这对于函数来说都是一个坏名字)。也许您要检查的是 if i == result 其中 result 是 ic5501 或 ic5502 的变量?如果我错了,请纠正我。

编辑

在检查 cmets 时,如果您尝试检查 i 是否是函数名称,我建议您作为基本开始(取决于这是否是一个大项目)创建一个包含所有函数名称的列表和将它们与函数相关联。

my_functions = [["function_1", function_1], ["function_2", function_2]]

我将向您推荐另一个问题,该问题的答案可以帮助您使用上述解决方案。

call list of function using list comprehension

【讨论】:

  • 你没有错,但我会添加更多的项目,而不仅仅是这两个
  • 当你说更多时,你的意思是一个确定的数字,或者你会添加更多但你不知道有多少?
【解决方案2】:

如果我在评论中给出的解释是正确的,允许用户调用您定义的函数的一种方法是将这些函数放入字典中,并以它们的名称作为关键字。然后您可以检查该字典以查看 i 是否属于它,并在适当的情况下调用该函数。

def buclick():
    i = ent.get()
    if i in user_callable_functions:
        user_callable_functions[i]()
    else:
        messagebox.showinfo("Error", "There is no product with this name")

#put this just above root.mainloop()
user_callable_functions = {
    "ic5501": ic5501,
    "ic5502": ic5502
}

【讨论】:

  • 如果。 in 这里正在进行会员测试。它在语义上与 for 语句中的 in 不同。
  • 感谢它对我有用,但如果比 for 更好,那么按摩会显示很多次
猜你喜欢
  • 2015-03-07
  • 2019-03-05
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-15
相关资源
最近更新 更多