Search code examples
pythontkintercombobox

Can I display test options in a tkinter Combobox, but still have it specify values?


I have a Combobox that I want to have text options for, but when that option is chosen I want the integer value to be returned to the tk.IntVar value. can I do this?

selected_diff = tk.IntVar()
        
nps_difficulty_combo_s = ttk.Combobox(tab3,text = ("Easiest","Moderate","Moderately Strenuous","Strenuous",
"Very Strenuous"), value=[1,2,3,4,5], width=25, 
textvariable=selected_diff)
    

I tried this, but it does not display the text as options. Thank you.


Solution

  • What you want to do isn't supported by the ttk combobox. When you do text=("Easiest", ...), tkinter sees text as a shorthand for the textvariable option.

    The simple solution is to create a mapping with a dictionary. You can then have the combobox display the keys of the dictionary, which you can later use to look up the value in the dictionary.