'TypeError: 'function' object is not subscriptable' 在 Python 3.4.3 中? [英] 'TypeError: 'function' object is not subscriptable' in Python 3.4.3?

查看:85
本文介绍了'TypeError: 'function' object is not subscriptable' 在 Python 3.4.3 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份食物菜单,库存和价格在不同的字典中.

I have a food menu and the stock and prices are in separate dictionaries.

食品库存:

Food_Stock = {
    'Chips' : 15,
    'Bagels' : 27,
    'Cookies' : 25}#Food Stock.

食品价格:

Food_Prices = {#Food Prices.
    'Chips' : 1,
    'Bagels' : 0.5,
    'Cookies' : 0.4}

食物菜单:

def Food_Menu():#The food menu.
    Top_Frame = Frame(root)
    Top_Frame.pack()
    Bottom_Frame = Frame(root)
    Bottom_Frame.pack(side = BOTTOM)

    tree['columns'] = ('Price', 'Qty', 'Print Receipt')#additional columns after the default '#0'

    tree.column('Price', stretch = 0, width = 100, anchor = E)#Price Column, tkinter.E aligns contents to the "east"
    tree.column('Qty', stretch = 0, width = 100, anchor = E)#Quantity Column
    tree.column('Print Receipt', stretch = 0, width = 100, anchor = E)#Print Receipt Column
    tree.heading('#0', text = "Item")# default column responsible for tree mechanics
    tree.heading('Price', text = "£")
    tree.heading('Qty', text = "Quantity")

    tree.insert('', 0, '_Chips_', values = (Food_Prices['Chips'], Food_Stock['Chips']), text = "Chips")#Parent, text goes to '#0', values go to tree['columns']
    tree.insert('_Chips_', 0, text = "Add to Order")#Child
    tree.insert('', 1, '_Bagels_', text = "Bagels", values = (Food_Prices['Bagels'], Food_Stock['Bagels']))#Parent.
    tree.insert('_Bagels_', 1, Add_Food_Item_To_Order_Button(), text = "Add to Order")#Child
    tree.insert('', 2, '_Cookies_', text = "Cookies", values = (Food_Prices['Cookies'], Food_Stock['Cookies']))#Parent.
    tree.insert('_Cookies_', 2, Add_Food_Item_To_Order_Button(), text = "Add to Order")#Child

    tree.pack()

GUI 通过将它链接到字典来显示股票和价格,或者如果它有效的话应该这样做.

The GUI displays the stock and price by linking it to the dictionary, or it should if it worked.

错误信息:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
  File "C:\Users\liam\Documents\BOA\Coursework\Python - Mr Naeem\Comp 4 Practical Project\Bar System.py", line 56, in Food_Button
Food_Menu()
  File "C:\Users\liam\Documents\BOA\Coursework\Python - Mr Naeem\Comp 4 Practical Project\Bar System.py", line 103, in Food_Menu
    tree.insert('', 0, '_Chips_', values = (Food_Prices['Chips'], Food_Stock['Chips']), text = "Chips")#Parent, text goes to '#0', values go to tree['columns']
TypeError: 'function' object is not subscriptable

细化:

GUI 将显示食物菜单 - def Food_Menu - 包含三列,价格"、数量"和打印收据".

The GUI will display a food menu - def Food_Menu - which has three columns, 'Price', 'Quantity' and 'Print Receipt'.

然后是通过访问字典 Food_Stock 和 Food_Prices 显示的树,例如 tree.insert("", 0, 'Chips', values...).它调用数据,然后应该显示它.这意味着它可以根据股票的下跌或上涨进行调整.

Then there are trees e.g tree.insert("", 0, 'Chips', values...) that are displayed by accessing the dictionaries Food_Stock and Food_Prices. It calls the data, then is supposed to display it. This means it can adjust to the stock going down or up.

推荐答案

'function' object is not subscriptable 意味着你正在做这样的事情:

'function' object is not subscriptable means you're doing something like this:

def foo(): pass
something = foo[1]

这意味着 Food_PricesFood_Stock 实际上是一个函数而不是变量.通过在导致错误的行之前添加简单的打印语句,应该很容易找出哪个.

What this means is that either Food_Prices or Food_Stock is actually a function rather than a variable. It should be easy to figure out which, by adding as simple print statement before the line that is causing the error.

print(Food_Prices, Food_Stock)

这很可能是因为您在代码中的其他位置有一个名为 Food_PricesFood_Stock 的函数,或者您重新分配了其中一个变量的值.

Most likely this is because you have a function somewhere else in your code named either Food_Prices or Food_Stock, or you've reassigned the value of one of those variables.

这篇关于'TypeError: 'function' object is not subscriptable' 在 Python 3.4.3 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆