[英]python tkinter - over writing label on button press
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)
I've created this tk window to display a shopping list of 10 items per category. 我创建了这个tk窗口,以显示每个类别10个项目的购物清单。 Each category has two list, item_name/item_price (they have been scraped off amazon.)
每个类别都有两个列表,即item_name / item_price(已从亚马逊网站刮下)。
When I run the program I can press the button and the list will display properly but if I press it again it adds new labels to the end of the previously made labels. 当我运行程序时,我可以按一下按钮,列表将正确显示,但是如果再次按一下,它将在以前制作的标签的末尾添加新标签。 My questions would be how do I make the program overwrite previous labels for example.
我的问题是例如如何使程序覆盖以前的标签。 Press "Buy Electronics" creates my labels as required, but pressing "Buy Books" after adds more labels.
按“购买电子产品”可根据需要创建我的标签,但在添加更多标签后按“购买书籍”。 I want to over write the "Buy Electronics" labels.
我想改写“购买电子产品”标签。 I figured it would be some kind of
global lbl
but unsure. 我认为这将是某种
global lbl
但不确定。
see the on the example with function have created, to do that you need to overwrite before positioning the current one there by using lbl["text"] = sv
请参阅具有已创建功能的示例上的,为此,您需要使用
lbl["text"] = sv
覆盖当前位置,然后覆盖
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["text"] = sv
sv.set(item_values)
Then create you Label
inside root
and mainloop
然后在
root
和mainloop
内创建Label
shop_window = Tk()
sv = StringVar()
lbl = Label(shop_window, height="2", textvariable=sv, anchor = NW)
lbl.grid(columnspan = 4)
shop_window = Tk()
You could create a frame for the labels 您可以为标签创建框架
f = Frame(shop_window)
f.grid(row=3, column=0, columnspan=4)
Then in the callback function destroy all children of the frame and create new ones 然后在回调函数中销毁框架的所有子节点并创建新的子节点
def button_pressed(item_name, item_price):
for widget in f.winfo_children():
widget.destroy()
for v1, v2 in zip(item_name, item_price):
item_values = '{} {}'.format(v1, v2)
Label(f, height="2", text=item_values).pack()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.