vendredi 15 février 2019

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. 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. Here is the code and screenshot.

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!




Aucun commentaire:

Enregistrer un commentaire