简体   繁体   中英

python 3.3 GUI Programing

I have this code written for a GUI program. The question is Question 1 – Chapter 14 – Question 6 –

Joe's Automotive performs the following routine maintenance services:

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

Write a GUI program with check buttons that allow the user to select any or all of these services. When the user clicks a button the total charges should be displayed. THIS IS MY CODE SO FAR, CAN someone tell me why it doesn't work??

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

That's because you never calculate the total. To fix the problem, you need to:

  1. Make a label to hold the total.

  2. Build a function that will get all of the IntVar s' values, sum them, and then alter the label's text to display the total.

  3. Bind somebutton to that function.

Below is a fixed version of the script:

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

The stuff I changed is in comment boxes.

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