打印后是否有重置标签的选项?(Python, Tkinter)



这是我的代码

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:我已经改变了这样的代码,但它仍然给我重复的答案作为标签。我所说的重复的意思是,例如,如果勾选box_1和box_2,它会返回给我勾选box_1和box_2。但是如果我取消选中checkbox_1并单击按钮,它返回给我checkbox_1, checkbox_2和checkbox_2(再次)。修改后的代码

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:感谢Tim Roberts先生的回答。你可以在下面找到他的答案。与此同时,我已经找到了另一个解决方案,为这个项目"StringVar"。这是我修改过的工作代码。

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

c不应该是全局的。您需要在每次单击按钮时从头开始重新构建它。adej也不需要是全局的

这工作。同时,删除cadej的全局定义。

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)

相关内容

  • 没有找到相关文章