1

I am having trouble with IF statements when using combo boxes when using Tkinter. When I want the program to select speed distance time, the program instead selects speed acceleration time and all the other formulas in the dropdown list. Please can you help me with this?

dropdown = Combobox(root)
dropdown['values']=("Speed, Distance, Time", "Speed, Acceleration, Time", "Gravitational Potential Energy(Ep)", "Kinetic Energy (Ek)", "Elastic Potential Energy (Ee)", "Energy/Work Done, Power, Time", "Energy/Work Done, Force, Distance", "Energy, Voltage, Charge", "Specific Heat Capacity", "Specific Latent Heat", "Efficency", "Pover, Voltage, Current", "Power, Current, Resistance", "Current, Charge, Time", "Voltage, Current, Resistance")
dropdown.pack()

def clicked():    
    if dropdown['values'][0] == dropdown_formulae[0][0]: # if the user has selected the first option
        simpleFormulaMenu(0)
    elif dropdown['values'][1] == dropdown_formulae[1][0]:
        simpleFormulaMenu(1)
    elif dropdown['values'][2] == dropdown_formulae[2][0]:
        pass
    elif dropdown['values'][3] == dropdown_formulae[3][0]:
        pass
    elif dropdown['values'][4] == dropdown_formulae[4][0]:
        pass
    elif dropdown['values'][5] == dropdown_formulae[5][0]:
        simpleFormulaMenu(5)
    elif dropdown['values'][6] == dropdown_formulae[6][0]:
        simpleFormulaMenu(6)
    elif dropdown['values'][7] == dropdown_formulae[7][0]:
        simpleFormulaMenu(7)
    elif dropdown['values'][8] == dropdown_formulae[8][0]:
        pass
    elif dropdown['values'][9] == dropdown_formulae[9][0]:
        pass
    elif dropdown['values'][10] == dropdown_formulae[1][0]:
        pass
    elif dropdown['values'][11]:
        simpleFormulaMenu(11)
    elif dropdown['values'][12]:
        simpleFormulaMenu(12)
    elif dropdown['values'][13]:
        simpleFormulaMenu(13)



button = tk.Button(text = "Submit", command = clicked)
button.pack()
stovfl
  • 14,998
  • 7
  • 24
  • 51

1 Answers1

1

Question: Get the index of a selected item in a ttk.Combobox


Core point: Use <seq>.index to get the index of the selected item


Reference


import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        
        self.combobox = combobox = ttk.Combobox(self)
        combobox['values'] = ("Speed, Distance, Time",
                              "Speed, Acceleration, Time",
                              "Gravitational Potential Energy(Ep)"
                              )
        combobox.pack()
        
        button = tk.Button(text="Submit", command=self.on_clicked)
        button.pack()
    
    def on_clicked(self, *event):
        selected = self.combobox.get()
        if selected:
            idx = self.combobox['values'].index(selected)
            print('simpleFormulaMenu({}), selected: {}'.format(idx, selected))
            
            if idx in (2, 3, 4):  # pass
                pass
            else:
                # simpleFormulaMenu(idx)
                pass


if __name__ == '__main__':
    App().mainloop()

Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6

Community
  • 1
  • 1
stovfl
  • 14,998
  • 7
  • 24
  • 51