0

im struggling to bind tkinter options menus by category. ive made a simple to run version of the problem below. it seems to work at first. eg, in the image below you can see it succesfully categorizing by the name dave.

enter image description here

######## UPDATE by passing a default value into my function i got it to work, now it only uses the default value. However, it always uses this default value. i assumed it would use the value i clicked on. but it always uses the default value.

from tkinter import *
data = {"names" :["dave" , "dave" , "dave" , "bob" , "sadie" , "jessica"] , "ages":[96,96,10,11,14,26] , "sexes":["m" , "f" , "m" , "m" , "f" , "f"]}


def age_selected_func(age_selected):
    print("age selected " +str(age_selected))
    sexes_object['menu'].delete(0, 'end')
    good_sexes = []
    for s, a in zip(data['sexes'], data['ages']):
        if a == age_selected:
            sexes_object['menu'].add_command(label=s, command=lambda s="none": sex_selected_func(s))   ## command=lambda a    <- this is the problem it doesnt know what a is      , but i need to pass a into it or its useless 
    ages_top_label_object.set(age_selected)


def name_selected_func(name_selected):
    print("name selected")
    ages_object['menu'].delete(0, 'end')
    good_ages = []
    for n, a in zip(data['names'], data['ages']):
        if n == name_selected:
            ages_object['menu'].add_command(label=a, command=lambda a="none": age_selected_func(a))   ## command=lambda a    <- this is the problem it doesnt know what a is      , but i need to pass a into it or its useless 
    names_top_label_object.set(name_selected)


window=Tk()
window.geometry("800x500+10+20")

names_top_label_object = StringVar(window)
names_top_label_object.set("choose a name")
names_object = OptionMenu(window, names_top_label_object, *data["names"] , command= lambda name_selected: name_selected_func(name_selected  ))
names_object.pack();
names_object.place(x=25, y=100);

ages_top_label_object = StringVar(window)
ages_top_label_object.set("choose an age")
ages_object = OptionMenu(window, ages_top_label_object, *data["ages"] , command= lambda model_c: model_selected(model_c ))
ages_object.pack();
ages_object.place(x=25, y=150);

sexes_top_label_object = StringVar(window)
sexes_top_label_object.set("choose an sex")
sexes_object = OptionMenu(window, sexes_top_label_object , *data["sexes"] , command= lambda make_c: make_selected(make_c  ))# , model_top_label_object, deriv_object , deriv_top_label_object ))
sexes_object.pack();
sexes_object.place(x=25, y=200);

new image

how can i bind these 3 drop down boxes?

  • I am not sure if I fully understood your question but if you want to pass a default parameter to lambda, you can do it using: `lambda a=a: age_selected_func(a)`. – Sriram Srinivasan Apr 13 '22 at 15:00
  • that kind of worked, but now it always passes in my default function. i assumed it would use the value i clicked on? – tgm_jack_learn Apr 13 '22 at 15:50
  • You need to give `a=a`, not `a="none"` if you want it to use the value you clicked on. Refer to [this](https://stackoverflow.com/questions/71798866/on-clicking-the-numbers-to-input-it-onto-the-entry-widget-it-always-adds-10-rat/71799742#71799742) answer to understand better why this works. – Sriram Srinivasan Apr 13 '22 at 16:19

0 Answers0