0
我有一個程序根據用戶輸入填充列表。這些列表實際上是具有2個值的對象列表:名稱和數量。在使用Tkinter的GUI中顯示對象列表
我目前正在運行tkinter並試圖在GUI中以可讀格式返回這些列表。這是我到目前爲止有:
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()
什麼,當我嘗試運行下面的情況是,我得到一個錯誤:「項目」對象不是可調用
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)
我怎樣才能打印這些列表在主GUI窗口中?
顯示完整的錯誤消息(Traceback)。順便說一句:使用CamelCase名稱的類('類Item(object)')和lower_case名稱的變量('容器')它有助於收回代碼中的類。你使用'item'作爲類的名字,後來用'item'中的變量。它會造成問題。 – furas
在添加到列表中之前,您會返回'addItem'。 'root.bind('',addItem())'正在調用函數並返回一個不綁定函數的item對象,這很可能是問題所在。 'root.bind('',addItem)'是你想要的 –
我用root.bind()算出來的。謝謝! – cstmxyz