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:
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:
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()The output in the dialog box is:
Output:Your total charges = $0.0
I don't think I am supposed to doif self.radio_var1.get() == 1: total = ticket * seniorWhat can I do different here?