Use radiobuttons to determine a total charge - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Use radiobuttons to determine a total charge (/thread-30916.html) |
Use radiobuttons to determine a total charge - SalsaBeanDip - Nov-13-2020 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: 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: I don't think I am supposed to doif self.radio_var1.get() == 1: total = ticket * seniorWhat can I do different here? RE: Use radiobuttons to determine a total charge - deanhystad - Nov-13-2020 None of these are ever True 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 * childBecause in this code you set the possible values for radio_var1 to be 5, 7 or 12. 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)You could change convert() to get the ticket value directly from radio_var1: def convert(self): total = self.radio_var1.get() * self.ticket_entry.get() tkinter.messagebox.showinfo('Total Charges', message + str(total))But I don't think it is a good idea to hardcode the ticket cost int the radio buttons. RE: Use radiobuttons to determine a total charge - SalsaBeanDip - Nov-13-2020 Thank you for the help. I was able to fix my problem def convert(self): message = 'Your total charges = $' ticket = self.ticket_entry.get() total = 0.00 total = float(self.radio_var1.get()) * float(self.ticket_entry.get()) tkinter.messagebox.showinfo('Total Charges', message + str(f'{total:.2f}')) |