Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

'TypeError: 'function' object is not subscriptable' in Python 3.4.3?

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

Food Stock:

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

Food Prices:

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

Food Menu:

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

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

The error message:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:Python34lib	kinter\__init__.py", line 1533, in __call__
return self.func(*args)
  File "C:UsersliamDocumentsBOACourseworkPython - Mr NaeemComp 4 Practical ProjectBar System.py", line 56, in Food_Button
Food_Menu()
  File "C:UsersliamDocumentsBOACourseworkPython - Mr NaeemComp 4 Practical ProjectBar 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

Elaboration:

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...