Home > Enterprise >  Is there any option to reset label after printing it ? [Python, Tkinter]
Is there any option to reset label after printing it ? [Python, Tkinter]

Time:12-04

this is my code

from tkinter import *

root = Tk()
root.title("MyTitle")
root.iconbitmap("icon.ico")
root.geometry("800x800")
c = []
feature1 = IntVar()
feature1.set(0)
feature2 = IntVar()
feature2.set(0)
feature3 = IntVar()
feature3.set(0)
Checkbutton(root, text="Pizza", variable=feature1).pack()
Checkbutton(root, text="Hamburger", variable=feature2).pack()
Checkbutton(root, text="Cola", variable=feature3).pack()
adej = "You've ordered"


def receipt():
    global c, adej
    if feature1.get() == 1:
        c.append("Pizza")
    if feature2.get() == 1:
        c.append("Hamburger")
    if feature3.get() == 1:
        c.append("Cola")
    adej = "You've ordered"
    for x in c:
        adej = adej   " "   x
    Label(root, text=adej).pack()
    adej = ""


button1 = Button(root, text="Get Receipt", command=receipt)
button1.pack()
root.mainloop()

edit_1: I've changed code like this but it still gives me duplicated answers as label. What i've meant as duplicate is for example if check box_1 and box_2 it returns me check box_1 and check box_2. But if i deselect check box_1 and click button it returns me check box_1, check box_2 and check box_2 (again). Here's changed code

from tkinter import *

root = Tk()
root.title("MyTitle")
root.iconbitmap("icon.ico")
root.geometry("800x800")
c = []
feature1 = IntVar()
feature1.set(0)
feature2 = IntVar()
feature2.set(0)
feature3 = IntVar()
feature3.set(0)
Checkbutton(root, text="Pizza", variable=feature1).pack()
Checkbutton(root, text="Hamburger", variable=feature2).pack()
Checkbutton(root, text="Cola", variable=feature3).pack()
adej = "You've ordered"


def receipt():
    global c, adej
    if feature1.get() == 1:
        c.append("Pizza")
    if feature2.get() == 1:
        c.append("Hamburger")
    if feature3.get() == 1:
        c.append("Cola")
    adej = "You've ordered"
    for x in c:
        adej = adej   " "   x
    label1.config(text=adej)


button1 = Button(root, text="Get Receipt", command=receipt)
button1.pack()
label1 = Label(root, text="")
label1.pack()
root.mainloop()

edit2: Thanks to Mr. Tim Roberts for the answer. You can find his answer as marked solution below. By the meantime i've found another solution for this project with "StringVar". Here's the changed working codes i've done.

from tkinter import *

root = Tk()
root.title("MyTitle")
root.iconbitmap("icon.ico")
root.geometry("250x150")
# creating string variables that will be connected to check boxes
feature1 = StringVar()
feature2 = StringVar()
feature3 = StringVar()

# attaching variables to checkboxes
Checkbutton(root, text="Pizza", variable=feature1, onvalue="Pizza ", offvalue="").pack()
Checkbutton(root, text="Hamburger", variable=feature2, onvalue="Hamburger ", offvalue="").pack()
Checkbutton(root, text="Cola", variable=feature3, onvalue="Cola ", offvalue="").pack()


def receipt():
    adej = "You've ordered "
    if len(feature1.get() feature2.get() feature3.get()) == 0:
        label1.config(text="You didn't order anything.")
    else:
        label1.config(text=adej   feature1.get()   feature2.get()   feature3.get())


button1 = Button(root, text="Get Receipt", command=receipt)
button1.pack()
label1 = Label(root, text="")
label1.pack()
root.mainloop()

CodePudding user response:

c should not be a global. You need to rebuild it from scratch every time the button is clicked. adej also does not need to be a global.

This works. Also, delete the global definitions of c and adej.

def receipt():
    c = []
    if feature1.get() == 1:
        c.append("Pizza")
    if feature2.get() == 1:
        c.append("Hamburger")
    if feature3.get() == 1:
        c.append("Cola")
    adej = "You've ordered "   ' '.join(c)
    label1.config(text=adej)
  • Related