[英]Dynamically Adding Tkinter Widgets
在我的计算机科学课上,我们被分配了制作电影亭的任务。 用户将物品添加到我以列表形式存储的购物车中。 加载结帐屏幕后,我想添加标签以显示用户购物车中有哪些物品。 我有以下代码:
class CheckOut(tk.Frame): #check out page
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#create and place base widgets
backGroundPicture = ImageTk.PhotoImage(Image.open("Background.jpg"))
backGroundLabel = tk.Label(self, image=backGroundPicture, borderwidth=0)
backGroundLabel.image = backGroundPicture
backGroundLabel.place(x=0, y=0, anchor=tk.NW)
checkOutLabel = tk.Label(self, text="Check Out", font=("Verdana", 48), bg="#001f33", fg="red")
checkOutLabel.place(relx=0.5, rely=0, anchor=tk.N)
returnButton = tk.Button(self, text="Return to Main Menu", bg="#4db8ff", fg="black",
command=lambda: controller.show_frame(StartPage))
returnButton.place(relx=1, rely=1, anchor=tk.SE)
def update(): #add new widgets displaying items purchase
print("debug")
itemLists = []
for each in movieTitles:
i = movieTitles.index(each)
if(adultTickets[i] != 0):
tempLabel = tk.Label(self, text="Adult Ticket: " + str(adultTickets[i]) + " " + each)
itemLists.append(tempLabel)
if(childTickets[i] != 0):
tempLabel = tk.Label(self, text="Child Ticket: " + str(childTickets[i]) + " " + each)
itemLists.append(tempLabel)
for i in itemLists:
i.place(x=100, y=200, anchor=tk.CENTER)
print(itemLists)
一切正常,除了当我尝试使用self的父级创建标签时,它是未定义的。 我想将它们放在init方法使用的同一帧中。 我将如何去获得自我? 我可以使用一些解决方法吗?
旁注:StackOverflow期望代码示例具有附加的4位缩进。 如果仅粘贴代码,则在文本框中选择它,然后在编辑器中(标记为{})单击“代码示例”按钮,它将添加缩进。
class CheckOut(tk.Frame): #check out page
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#create and place base widgets
backGroundPicture = ImageTk.PhotoImage(Image.open("Background.jpg"))
backGroundLabel = tk.Label(self, image=backGroundPicture, borderwidth=0)
backGroundLabel.image = backGroundPicture
backGroundLabel.place(x=0, y=0, anchor=tk.NW)
checkOutLabel = tk.Label(self, text="Check Out", font=("Verdana", 48), bg="#001f33", fg="red")
checkOutLabel.place(relx=0.5, rely=0, anchor=tk.N)
returnButton = tk.Button(self, text="Return to Main Menu", bg="#4db8ff", fg="black",
command=lambda: controller.show_frame(StartPage))
returnButton.place(relx=1, rely=1, anchor=tk.SE)
...
Python中类的方法将对象作为第一个参数传递给方法。 按照惯例,该参数名为self
,但实际上,您可以给它起任何名字。*因此,将self
作为参数添加到update
方法中。 我想您还想将电影标题作为参数传递给update
方法。
...
def update(self, movieTitles): #add new widgets displaying items purchase
print("debug")
itemLists = []
for each in movieTitles:
i = movieTitles.index(each)
if(adultTickets[i] != 0):
tempLabel = tk.Label(self, text="Adult Ticket: " + str(adultTickets[i]) + " " + each)
itemLists.append(tempLabel)
if(childTickets[i] != 0):
tempLabel = tk.Label(self, text="Child Ticket: " + str(childTickets[i]) + " " + each)
itemLists.append(tempLabel)
for i in itemLists:
i.place(x=100, y=200, anchor=tk.CENTER)
print(itemLists)
这样,在实例化对象之后,可以应用如下方法:
# instantiate object
my_frame = CheckOut(parent, controller)
# apply "update"
my_frame.update(my_movie_titles)
# The previous line has the same effect as this:
CheckOut.update(my_frame, my_movie_titles)
注意:我没有测试代码,只是修复了您在那儿遇到的小语法错误。 乍一看,这些电影的标签似乎都具有相同的位置。
同样,正如用户“ zondo”所建议的那样,到处都是lambda,使代码混乱; 摆脱它们会使代码更易于阅读。 可以用functools
模块中的partial
函数代替这样的简单事件:
from functools import partial
returnButton = tk.Button(self, ...,
command=partial(controller.show_frame, StartPage))
this
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.