Hello. I am trying to write a GUI program that calculates total ticket prices. It allows the user to select a ticket category from a set of radiobuttons and enter the number of tickets into an Entry widget. Then an info dialog box displays the total charge. However, it just keeps displaying $0.0 as my total.
What am I doing wrong in my code? Why does it only display 0.0 and how can I display it with 2 decimal places?
The ticket prices are:
Senior: $7
Adult: $12
Child: $5
Here is my code:
The output in the dialog box is:
What can I do different here?
What am I doing wrong in my code? Why does it only display 0.0 and how can I display it with 2 decimal places?
The ticket prices are:
Senior: $7
Adult: $12
Child: $5
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import tkinter import tkinter.messagebox class TicketCalculatorGUI: def __init__( self ): self .main_window = tkinter.Tk() self .main_window.title( 'Ticket Price Calculator' ) self .top_frame = tkinter.Frame( self .main_window) self .mid_frame = tkinter.Frame( self .main_window) self .bottom_frame = tkinter.Frame( self .main_window) self .radio_var1 = tkinter.IntVar() self .radio_var1. set ( 1 ) # create radio buttons in top frame self .rb_seniors = tkinter.Radiobutton( self .top_frame, text = 'Senior (>65)' , variable = self .radio_var1, value = 7 ) self .rb_adults = tkinter.Radiobutton( self .top_frame, text = 'Adult (15-65)' , variable = self .radio_var1, value = 12 ) self .rb_child = tkinter.Radiobutton( self .top_frame, text = 'Child (5-15)' , variable = self .radio_var1, value = 5 ) self .rb_seniors.pack() self .rb_adults.pack() self .rb_child.pack() # in mid frame use entry widget to get number of tickets self .prompt_label = tkinter.Label( self .mid_frame, text = 'Enter the number of tickets:' ) self .ticket_entry = tkinter.Entry( self .mid_frame, width = 10 ) self .prompt_label.pack(side = 'left' ) self .ticket_entry.pack(side = 'left' ) # button in bottom frame should display total charges self .calc_button = tkinter.Button( self .bottom_frame, text = 'Display Charges' , command = self .convert) self .quit_button = tkinter.Button( self .bottom_frame, text = 'Quit' , command = self .main_window.destroy) self .calc_button.pack(side = 'left' ) self .quit_button.pack(side = 'left' ) self .top_frame.pack() self .mid_frame.pack() self .bottom_frame.pack() tkinter.mainloop() def convert( self ): message = 'Your total charges = $' ticket = self .ticket_entry.get() total = 0.00 senior = 7 adult = 12 child = 5 if self .radio_var1.get() = = 1 : total = ticket * senior elif self .radio_var1.get() = = 1 : total = ticket * adult elif self .radio_var1.get() = = 1 : total = ticket * child tkinter.messagebox.showinfo( 'Total Charges' , message + str (total)) if __name__ = = '__main__' : tickets = TicketCalculatorGUI() |
Output:Your total charges = $0.0
I don't think I am supposed to do1 2 |
if self .radio_var1.get() = = 1 : total = ticket * senior |