Check OptionMenu selection and update GUI
Question: I'm working on a class project and I'm trying to take it beyond the
requirements a little here (I'm doing my own homework, just need help
improving it!) so I want to update the GUI based on certain selections the
user makes instead of just having all irrelevent options available all the
time (requirements are to just present the options).

I'm still new to Python and even more new to Tkinter so my only attempt has
been the following:

    
    
    #Step Type
    ttk.Label(mainframe, text = "Step Type").grid(column = 1, row = 16)
    type_entry = OptionMenu(mainframe, StepType, "Kill", "Explore" , "Conversation")
    type_entry.grid(column = 2, row = 16, sticky = (E))
    
    #Step Goal
    if StepType.get() == "Kill":
       ttk.Label(mainframe, text = "Required Kills").grid(column = 1, row = 17)
       goal_entry = ttk.Entry(mainframe, width = 20, textvariable = StepGoal)
       goal_entry.grid(column = 2, row = 17, sticky = (E))
    elif StepType.get() == "Explore":
       ttk.Label(mainframe, text = "Location ID").grid(column = 1, row = 17)
       goal_entry = ttk.Entry(mainframe, width = 20, textvariable = StepGoal)
       goal_entry.grid(column = 2, row = 17, sticky = (E))
    elif StepType.get() == "Conversation":
       ttk.Label(mainframe, text = "NPC ID").grid(column = 1, row = 17)
       goal_entry = ttk.Entry(mainframe, width = 20, textvariable = StepGoal)
       goal_entry.grid(column = 2, row = 17, sticky = (E))
    

Obviously what I want to do here is when the user selects one of the options
from the menu, to display the corresponding entry box and label instead of
having all 3 all the time.

Also looking for the same situation for `CheckButton`


Answer: Full working example: tested od 2.7.5 and 3.3.2

It use `command=` in `OptionMenu` to call function when user changed option.

    
    
    import tkinter as ttk
    
    #----------------------------------------------------------------------
    
    def on_option_change(event):
        selected = step_type.get()
    
        if selected == "Kill":
            goal_label['text'] = "Required Kills"
        elif selected == "Explore":
           goal_label['text'] = "Location ID"
        elif selected == "Conversation":
           goal_label['text'] = "NPC ID"
    
        # show label and entry
        #goal_label.grid(column=1, row=17)
        #goal_entry.grid(column=2, row=17, sticky='E')
    
    #----------------------------------------------------------------------
    
    mainframe = ttk.Tk()
    
    # Step Type
    
    step_type = ttk.StringVar() # there is the rule: variable name lowercase with _
    ttk.Label(mainframe, text="Step Type").grid(column=1, row=16)
    type_entry = ttk.OptionMenu(mainframe, step_type, "Kill", "Explore" , "Conversation", command=on_option_change)
    type_entry.grid(column=2, row=16, sticky='E')
    step_type.set("Kill")
    
    # Step Goal
    
    step_goal = ttk.StringVar()
    goal_label = ttk.Label(mainframe, text="Required Kills")
    goal_label.grid(column=1, row=17)
    goal_entry = ttk.Entry(mainframe, width=20, textvariable=step_goal)
    goal_entry.grid(column=2, row=17, sticky='E')
    
    # hide label and entry
    #goal_label.grid_forget()
    #goal_entry.grid_forget()
    
    # --- star the engine ---
    
    mainframe.mainloop()
    

BTW: you can use `grid()` and `grid_forget()` to show and hide elements.

* * *

**EDIT:** example with `Radiobutton` using `trace` on `StringVar`

    
    
    import tkinter as ttk
    
    #----------------------------------------------------------------------
    
    def on_variable_change(a,b,c): # `trace` send 3 argument to `on_variable_change`
    
        #print(a, b, c)
    
        selected = step_type.get()
    
        if selected == "Kill":
            goal_label['text'] = "Required Kills"
        elif selected == "Explore":
           goal_label['text'] = "Location ID"
        elif selected == "Conversation":
           goal_label['text'] = "NPC ID"
    
    #----------------------------------------------------------------------
    
    mainframe = ttk.Tk()
    
    # Step Type
    
    step_type = ttk.StringVar() # there is the rule: variable name lowercase with _
    
    ttk.Label(mainframe, text="Step Type").grid(column=1, row=16)
    
    ttk.Radiobutton(mainframe, text="Kill", value="Kill", variable=step_type).grid(column=2, row=16, sticky='E')
    ttk.Radiobutton(mainframe, text="Explore", value="Explore", variable=step_type).grid(column=3, row=16, sticky='E')
    ttk.Radiobutton(mainframe, text="Conversation", value="Conversation", variable=step_type).grid(column=4, row=16, sticky='E')
    
    step_type.set("Kill")
    # use `trace` after `set` because `on_variable_change` use `goal_label` which is not created yet.
    step_type.trace("w", on_variable_change)
    
    # Step Goal
    
    step_goal = ttk.StringVar()
    goal_label = ttk.Label(mainframe, text="Required Kills")
    goal_label.grid(column=1, row=17)
    goal_entry = ttk.Entry(mainframe, width=20, textvariable=step_goal)
    goal_entry.grid(column=2, row=17, sticky='E')
    
    # --- star the engine ---
    
    mainframe.mainloop()
    

