validation - Check OptionMenu selection and update GUI -


i'm working on class project , i'm trying take beyond requirements little here (i'm doing own homework, need improving it!) want update gui based on selections user makes instead of having irrelevent options available time (requirements present options).

i'm still new python , more new tkinter attempt has been 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 want here when user selects 1 of options menu, display corresponding entry box , label instead of having 3 time.

also looking same situation checkbutton

full working example: tested od 2.7.5 , 3.3.2

it use command= in optionmenu call function when user changed option.

import tkinter 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 , 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 rule: variable name lowercase _ 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 , entry #goal_label.grid_forget() #goal_entry.grid_forget()  # --- star engine ---  mainframe.mainloop() 

btw: can use grid() , grid_forget() show , hide elements.


edit: example radiobutton using trace on stringvar

import tkinter ttk  #----------------------------------------------------------------------  def on_variable_change(a,b,c): # `trace` send 3 argument `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 rule: variable name lowercase _  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` 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 engine ---  mainframe.mainloop() 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -