python 3.3 GUI编程

python 3.3 GUI编程,python,tkinter,python-3.3,Python,Tkinter,Python 3.3,我为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程序,允许用户选择任何或所有这些服务。

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

那是因为你从来不计算总数。要解决此问题,您需要:

  • 制作一个标签来保存总数

  • 构建一个函数,该函数将获取所有
    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()
    

    我更改的内容在评论框中。

    你能解释一下你所说的“它不工作”是什么意思吗?错误?可能是意外的输出?是的,很抱歉,我可以打开屏幕,但它不会合计我检查的所有内容,也不会显示合计屏幕您在
    somebutton=Button(root,text=“total”)中什么也没做。