Search code examples
pythoncheckboxtkinterradio-buttonmessage

How to display GUI message indicating which checkbutton(s) and radiobutton have been selected in tkinter in Python?


I am super new to Python and programming in general. I'm messing around in tkinter trying to make a silly and simple program so I can get more comfortable with it.

What I am doing with my program is asking for the user's name, age, if they remember certain events, and if they feel old or not. Like I said, it is supposed to be a light hearted program just to put my skills to the test.

Now I wonder, how to display GUI message indicating which checkbutton(s) and radiobutton have been selected in tkinter in Python?

Here is the code and a screenshot. I'll include a screenshot of my results but I can get the name and age to work like it should but can't get the message to appear correctly in the GUI with the checkboxes or radio button.

from tkinter import *

class MyFrame(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.geometry("600x400")
        self.master.title("How to tell if you're old")
        self.event = NONE
        self.old = NONE
        self.grid()

    #user name and age
        self.prompt = Label(self, text="What's your name?")
        self.prompt.grid(row=0, column=0, pady=5)

        self.input_name = Entry(self)
        self.input_name.grid(row=0, column=1, pady=5)

        self.prompt = Label(self, text="How old are you?")
        self.prompt.grid(row=2, column=0, pady=5)

        self.input_age = Entry(self)
        self.input_age.grid(row=2, column=1, pady=10)

    #user asks user if they remember certain events
        self.prompt = Label(self, text="Which of these events do"
                                   "you remember (may select more than one)?")
        self.prompt.grid(row=3, columnspan=5, pady=5)

        self.wheel_event = IntVar()
        self.check_wheel_event = Checkbutton(self, text="Invention of the wheel",
                                         variable=self.wheel_event, command=self.set_response_event)
        self.check_wheel_event.grid(row=4, column=0, padx=5)

        self.firstFlight_event = IntVar()
        self.check_firstFlight_event = Checkbutton(self, text="First flight",
                                               variable=self.firstFlight_event, command=self.set_response_event)
        self.check_firstFlight_event.grid(row=4, column=1, padx=5)

        self.Berlin_Wall_event = IntVar()
        self.check_Berlin_Wall_event = Checkbutton(self, text="Berlin Wall",
                                               variable=self.Berlin_Wall_event, command=self.set_response_event)
        self.check_Berlin_Wall_event.grid(row=4, column=2, padx=5)

        self.millennium_event = IntVar()
        self.check_millennium_event = Checkbutton(self, text="Millennium",
                                              variable=self.millennium_event, command=self.set_response_event)
        self.check_millennium_event.grid(row=4, column=3, padx=5)

    #user answers if they think they're old and if they want to know how
        # old they'll be in 10, 15, or 20 years
        self.prompt = Label(self, text="Do you consider yourself old?")
        self.prompt.grid(row=5, column=0, pady=5)

        self.feel_old = IntVar()
        self.feel_old.set(4)

        self.not_sure_old = Radiobutton(self, text="Not sure",
                                    variable=self.feel_old, value="0")
        self.not_sure_old.grid(row=6, column=0)

        self.no_old = Radiobutton(self, text="No",
                              variable=self.feel_old, value="1")
        self.no_old.grid(row=6, column=1)

        self.yes_old = Radiobutton(self, text="Yes",
                               variable=self.feel_old, value="2")
        self.yes_old.grid(row=6, column=2)

    #submit button
        self.button_submit = Button(self, text='Submit',
                                command=self.submit_click)
        self.button_submit.grid
        self.button_submit.grid(row=9, column=3, padx=10)

        self.my_name = StringVar()
        self.message = Label(self, textvariable=self.my_name)
        self.message.grid(columnspan=2, pady=10)

        self.my_age = StringVar()
        self.message = Label(self, textvariable=self.my_age)
        self.message.grid(columnspan=2, pady=10)

    #response
    def set_response_event(self):
        #remembering events
        if self.wheel_event.get() == 1:
            self.event = "wheel"
        elif self.firstFlight_event.get() == 1:
            self.event = "firstFlight"
        elif self.Berlin_Wall_event.get() == 1:
            self.event = "Berlin_Wall"
        elif self.millennium_event.get() == 1:
            self.event = "millennium"

    def set_response_old(self):
        #feeling old
        if self.not_sure_old.get() == "0":
            self.old = "not_sure_old"
        elif self.no_old.get() == "1":
            self.old = "no_old"
        elif self.yes_old.get() == "2":
            self.old = "yes_old"

    def submit_click(self):
        output_message = 'Well ' + self.input_name.get() + ', to begin with you are ' + self.input_age.get() + '.\n'
        output_message += 'You remember the ' + self.event +'.\n'
        output_message += 'This means you are ' + self.old + '.'

        self.my_name.set(output_message)

frame05 = MyFrame()
frame05.mainloop()

Here is what I get: enter image description here

I realize I'm probably doing this the hard way but I feel like I'm really close. Thank you for your help!


Solution

  • First off, you are never calling set_response_old(self) that I can tell. Secondly you are trying to compare strings to integers, which doesn't get caught in your if statements. Try rewriting the last two functions like this:

        def set_response_old(self):
            #feeling old
            if self.feel_old.get() == 0:
                self.old = "not sure old"
            elif self.feel_old.get() == 1:
                self.old = "no old"
            elif self.feel_old.get() == 2:
                self.old = "yes old"
    
        def submit_click(self):
            self.set_response_old()
            output_message = 'Well ' + self.input_name.get() + ', to begin with you are ' + self.input_age.get() + '.\n'
            output_message += 'You remember the ' + self.event + '.\n'
            output_message += 'This means you are ' + self.old + '.'
            self.my_name.set(output_message)
    

    When I run this it outputs

    This means you are yes old.

    Which I think is what you were going for.

    @EDIT

    As for getting the list of remembered events to show...You can add self.events_remembered = [] to your __init__ section to create a place to store the data (it's not required to be in the init section as the code below would make it anyways, but it's good practice!) and then change your set_response function as such:

        def set_response_event(self):
        #remembering events
        self.events_remembered = [] # reset each time we are determining pressed events (so we don't just keep adding duplicates, and also remove any unchecked boxes)
        if self.wheel_event.get() == 1:
            self.events_remembered.append("wheel")
        if self.firstFlight_event.get() == 1:
            self.events_remembered.append("firstFlight")
        if self.Berlin_Wall_event.get() == 1:
            self.events_remembered.append("Berlin Wall")
        if self.millennium_event.get() == 1:
            self.events_remembered.append("millennium")
    

    You are correct in thinking that the if will only catch the first one, but there is no rule against having a series of if statements like shown!

    Finally you can update your submit_click function to search for this information and display the formatted list

    def submit_click(self):
        self.set_response_old()
        self.set_response_event() # use the function we just updated
        print(self.events_remembered)
        output_message = 'Well ' + self.input_name.get() + ', to begin with you are ' + self.input_age.get() + '.\n'
        output_message += 'You remember the ' + ", ".join(self.events_remembered) + '.\n'
        output_message += 'This means you are ' + self.old + '.'
        self.my_name.set(output_message)
    

    Here we use ", ".join(iterable_list) to convert our python list to a nice comma separated list instead!

    If this solved your question I would appreciate if you could accept the answer by clicking the check mark next to the post! If you really just like me you can click to upvote me too XD