0

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()
3
  • 1
    Can you please explain what do you mean by "it doesn't work"? An error? Unexpected output, perhaps? Commented Dec 5, 2013 at 15:20
  • yes sorry about that, I can the screen to come up, but it will not then total everything I check off and does not display a total screen Commented Dec 5, 2013 at 15:23
  • you are doing nothing in somebutton=Button(root, text="Total") Commented Dec 5, 2013 at 15:25

1 Answer 1

2

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 IntVars' 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.