I have a program that populates lists based on user input. These lists are actually lists of objects that have 2 values: name and amount.
I'm currently running tkinter and trying to return these lists in a readable format in the GUI. Here's what I have so far:
from tkinter import *
from tkinter import ttk
Containers =[]
Lids = []
Wicks = []
Labels = []
Misc = []
Items = [Containers, Lids, Wicks, Labels, Misc]
class item(object):
#Constructor
def __init__(self, name, amount):
self.name = name
self.amount = amount
#Accessors
def getName(self):
return self.name
def getAmount(self):
return self.amount
def addItem(*args):
s = name.get()
global new_item
new_item = item(s, 0)
return new_item
Containers.append(new_item)
name = StringVar()
amount = StringVar()
remove_item = StringVar()
add_amount = StringVar()
subtract_amount = StringVar()
...#Leaving out all the frame information
t = Text(mainframe, width=60)
for item in Containers:
t.insert(END, item)
t.grid(column = 1, columnspan = 3, row = 10, padx = 5, pady = 10)
root.bind('<Return>', addItem())
root.mainloop()
What happens when I try to run the following is that I get an error: 'item' object is not callable
t = Text(mainframe, width=60)
for item in Containers:
t.insert(END, item)
t.grid(column = 1, columnspan = 3, row = 10, padx = 5, pady = 10)
How can I print these lists in the main GUI window?
class Item(object)) and lower_case names for variables (containers) It helps recegnize classes in code. You useitemas class name and later as variable infor item. It can make problem.addItemyou return before even adding it to the list.root.bind('<Return>', addItem())is calling the function and returning an item object not binding the function, this is most likely the issue.root.bind('<Return>', addItem)is what you want