TypeError: function object is not subscriptable in Python 3.4.3?

2024/7/7 6:32:19

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 Columntree.column('Print Receipt', stretch = 0, width = 100, anchor = E)#Print Receipt Columntree.heading('#0', text = "Item")# default column responsible for tree mechanicstree.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")#Childtree.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")#Childtree.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")#Childtree.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:\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_Menutree.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.

Answer

'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.

https://en.xdnf.cn/q/120549.html

Related Q&A

Insert a value in date format dd-mm-yyyy in dictionary in python

I am creating a dictionary having some values including a date of birth of a person. But when I run my code, it is giving an error "datetime.datetime has no attribute datetime" . Here is my …

Collisions arent registering with Python Turtles

Im creating a simple 2D shooter following an online tutorial, and the enemy sprites (except 1, there are 5 total) are not abiding by my collision code. Everything works except the bullet object that th…

Function should clean data to half the size, instead it enlarges it by an order of magnitude

This has been driving me nuts all week weekend. I am trying merge data for different assets around a common timestamp. Each assets data is a value in dictionary. The data of interest is stored in lists…

is it possible to add colors to python output? [duplicate]

This question already has answers here:How do I print colored text to the terminal?(66 answers)Closed 10 years ago.so i made a small password strength tester for me, my friends and my family, as seen …

Tkinter Function attached to Button executed immediately [duplicate]

This question already has answers here:How can I pass arguments to Tkinter buttons callback command?(2 answers)Closed 8 years ago.What I need is to attach a function to a button that is called with a …

Error!!! cant concatenate the tuple to non float

stack = []closed = []currNode = problem.getStartState()stack.append(currNode)while (len(stack) != 0):node = stack.pop()if problem.isGoalState(node):print "true"closed.append(node)else:child =…

Using R to fit data from a csv with a gamma function?

Using the following data: time_stamp,secs,count 2013-04-30 23:58:55,1367366335,32 2013-04-30 23:58:56,1367366336,281 2013-04-30 23:58:57,1367366337,664 2013-04-30 23:58:58,1367366338,1255 2013-04-30…

regex multiple string match in a python list

I have a list of strings like this:["ra", "dec", "ra-error", "dec-error", "glat", "glon", "flux", "l", "b"]I ne…

python IndentationError: expected an indented block [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

how to tell an infinite loop to end once one number repeats twice in a row (in python 3.4)

The title says it all. I have an infinite loop of randomly generated numbers from one to six that I need to end when 6 occurs twice in a row.