我为GUI程序编写了这段代码。问题是问题1 - 第14章 - 问题6 -
Joe's Automotive执行以下日常维护服务:
Oil Change--$26.00
Lube Job--#18.00
Radiator Flush--#30.00
Transmission Flush--$80.00
Inspection--$15.00
Muffler replacement--$100.00
Tire Rotation--$20.00
编写带有检查按钮的GUI程序,允许用户选择任何或所有这些服务。当用户单击按钮时,应显示总费用。这是我的代码,所以有人可以告诉我为什么它不起作用?
##############################################################################
#
#
# Name: Marc DiFalco
#
# Lab: 13
#
# Description: GUI Lab on instructions
#
#
#
# Inputs: Type of job
# Outputs: Job done and price
# Variables:CheckVar1,CheckVar2,CheckVar3,CheckVar4,CheckVar5,CheckVar6,
# CheckVar7, totalvalue
#
#
#
#
#
###############################################################################
#import
from tkinter import *
root=Tk()
root.title("Some GUI")
root.geometry("400x700")
CheckVar1=IntVar()
CheckVar2=IntVar()
CheckVar3=IntVar()
CheckVar4=IntVar()
CheckVar5=IntVar()
CheckVar6=IntVar()
CheckVar7=IntVar()#Set the variables
totalvalue=0
#The user can check off which jobs they would like to purchase
Oil=Checkbutton(root,text="Oil Change 20.00",variable=CheckVar1,onvalue=20\
,offvalue=0,height=5,width=20)
Lube=Checkbutton(root,text="Lube Job 18.00",variable=CheckVar2,onvalue=18\
,offvalue=0,height=5,width=20)
RadiatorFlush=Checkbutton(root,text="Radiator Flush--$30.00",variable=CheckVar3,onvalue=30\
,offvalue=0,height=5,width=20)
Transmission=Checkbutton(root,text="Transmission Flush--80.00",variable=CheckVar4,onvalue=80\
,offvalue=0,height=5,width=20)
Inspection=Checkbutton(root,text="Inspection--15.00",variable=CheckVar5,onvalue=15\
,offvalue=0,height=5,width=20)
Muffler=Checkbutton(root,text="Muffler replacement--100.00",variable=CheckVar6,onvalue=100\
,offvalue=0,height=5,width=20)
Tire=Checkbutton(root,text="Tire Rotation--20.00",variable=CheckVar7,onvalue=20\
,offvalue=0,height=5,width=20)
somebutton=Button(root, text="Total")
#Call each job
Oil.pack()
Lube.pack()
RadiatorFlush.pack()
Transmission.pack()
Inspection.pack()
Muffler.pack()
Tire.pack()
somebutton.pack()
#main loop
root.mainloop()
答案 0 :(得分:2)
那是因为你从未计算总数。要解决此问题,您需要:
制作标签以保留总数。
构建一个函数,该函数将获取所有IntVar
s'值,求它们,然后更改标签的文本以显示总数。
将somebutton
绑定到该功能。
以下是该脚本的固定版本:
from tkinter import *
root=Tk()
root.title("Some GUI")
root.geometry("400x700")
CheckVar1=IntVar()
CheckVar2=IntVar()
CheckVar3=IntVar()
CheckVar4=IntVar()
CheckVar5=IntVar()
CheckVar6=IntVar()
CheckVar7=IntVar()
totalvalue=0
Oil=Checkbutton(root,text="Oil Change 20.00",variable=CheckVar1,onvalue=20\
,offvalue=0,height=5,width=20)
Lube=Checkbutton(root,text="Lube Job 18.00",variable=CheckVar2,onvalue=18\
,offvalue=0,height=5,width=20)
RadiatorFlush=Checkbutton(root,text="Radiator Flush--$30.00",variable=CheckVar3,onvalue=30\
,offvalue=0,height=5,width=20)
Transmission=Checkbutton(root,text="Transmission Flush--80.00",variable=CheckVar4,onvalue=80\
,offvalue=0,height=5,width=20)
Inspection=Checkbutton(root,text="Inspection--15.00",variable=CheckVar5,onvalue=15\
,offvalue=0,height=5,width=20)
Muffler=Checkbutton(root,text="Muffler replacement--100.00",variable=CheckVar6,onvalue=100\
,offvalue=0,height=5,width=20)
Tire=Checkbutton(root,text="Tire Rotation--20.00",variable=CheckVar7,onvalue=20\
,offvalue=0,height=5,width=20)
##################################################################
total_lbl = Label(root)
def click():
total = 0
for var in (CheckVar1, CheckVar2, CheckVar3, CheckVar4, CheckVar5, CheckVar6, CheckVar7):
total += var.get()
total_lbl.config(text="${}.00".format(total))
somebutton=Button(root, text="Total", command=click)
###################################################################
Oil.pack()
Lube.pack()
RadiatorFlush.pack()
Transmission.pack()
Inspection.pack()
Muffler.pack()
Tire.pack()
somebutton.pack()
###############
total_lbl.pack()
###############
root.mainloop()
我更改的内容在评论框中。