Python Forum
Use radiobuttons to determine a total charge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use radiobuttons to determine a total charge
#1
Question 
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:
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()
The output in the dialog box is:
Output:
Your total charges = $0.0
I don't think I am supposed to do
1
2
if self.radio_var1.get() == 1:
            total = ticket * senior
What can I do different here?
Reply
#2
None of these are ever True
1
2
3
4
5
6
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
Because in this code you set the possible values for radio_var1 to be 5, 7 or 12.
1
2
3
4
5
6
7
8
9
10
11
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:
1
2
3
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.
SalsaBeanDip likes this post
Reply
#3
Thank you for the help. I was able to fix my problem

1
2
3
4
5
6
7
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}'))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Default Values for radiobuttons xuraax 2 7,088 May-17-2020, 06:43 PM
Last Post: xuraax
  [Tkinter] Connect Toplevel Radiobuttons to root Label/Entry widgets iconit 2 3,493 Apr-28-2020, 06:50 AM
Last Post: iconit
  2 sets of radiobuttons Chuck_Norwich 4 3,829 Dec-02-2019, 07:31 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020