def button_pressed(item_name, item_price):
global lbl
for v1, v2 in zip(item_name, item_price):
item_values = '{} {}'.format(v1, v2)
sv = StringVar()
lbl = Label(shop_window, height="2", textvariable=sv, anchor = NW).grid(columnspan = 4)
sv.set(item_values)
# Create initial shopping cart window
shop_window = Tk()
shop_window.title('Welcome to the Outlet')
shop_window.geometry("1200x900")
shop_window.resizable(0, 0)
introduction_text = Label(shop_window, text = 'Welcome to the Shopping Outlet', font = ('Arial', 30))
electronics_button = Button(shop_window, text = 'Buy Electronics', font = ('Arial', 18), command = lambda:button_pressed(electronics_name, electronics_price))
books_button = Button(shop_window, text = 'Buy Books', font = ('Arial', 18), command = lambda:button_pressed(books_name, books_price))
kitchen_button = Button(shop_window, text = 'Buy Kitchen', font = ('Arial', 18), command = lambda:button_pressed(kitchen_name, kitchen_price))
monitors_button = Button(shop_window, text = 'Buy Kitchen', font = ('Arial', 18), command = lambda:button_pressed(monitors_name, monitors_price))
introduction_text.grid(row = 0, column = 0, columnspan = 4, sticky = N )
electronics_button.grid(row = 2, column = 0)
books_button.grid(row = 2, column = 1)
kitchen_button.grid(row = 2, column =2)
monitors_button.grid(row = 2, column = 3)
我创建了这个tk窗口来显示每个类别10个项目的购物列表。每个类别都有两个列表,item_name/item_price(它们是从amazon上删除的)
当我运行程序时,我可以按下按钮,列表将正确显示,但如果我再按一次,它会在先前制作的标签末尾添加新标签。例如,我的问题是如何使程序覆盖以前的标签。但“购买我的电子产品”后添加了更多的标签。我想把“买电子产品”的标签写多了。我想这可能是某种global lbl
,但不确定。在
你可以为标签创建一个框架
然后在回调函数中销毁框架的所有子级并创建新的子级
^{pr2}$请参阅函数已创建的示例上的,要做到这一点,您需要在使用
lbl["text"] = sv
在那里定位当前的一个之前覆盖它然后在}内创建
^{pr2}$root
和{Label
相关问题 更多 >
编程相关推荐