简体   繁体   中英

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. Each category has two list, item_name/item_price (they have been scraped off amazon.)

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.

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

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM