Home > Enterprise >  tkinter multiple drowdown arguments
tkinter multiple drowdown arguments

Time:09-08

So I am able to use a single variable ie. Voltage to print True

I am trying to use Voltage and Phase as arguments to print True but I am not having any luck

Here is my code so far:

from tkinter import *

def edit_or_retrieve():
    Window = Tk()
    Window.title("Main Menu")
    Window.geometry("550x100")

    Box = StringVar(Window)
    Box.set("VOLTAGE")
    Voltage = ["120", "208", "240", "277", "480"] 
    Dropdown = OptionMenu(Window, Box, *Voltage, command=lambda x: function(x, Window))                                    
    Dropdown.pack(side = "left")

    Box = StringVar(Window)
    Box.set("PHASE")
    Phase = ["SINGLE PHASE", "THREE PHASE"] 
    Dropdown = OptionMenu(Window, Box, *Phase, command=lambda x: function(x, Window))                                    
    Dropdown.pack(side = "left")
    Window.mainloop()

def function(value, Window): 
    if value == '120':
        print(True)

edit_or_retrieve()

This will bring up a drop down box for Voltage and Phase, when I select Voltage I get True

Thanks for the help

CodePudding user response:

You need to use different tkinter variables (StringVar) for the two dropdowns and pass their values to function():

from tkinter import *

def edit_or_retrieve():
    Window = Tk()
    Window.title("Main Menu")
    Window.geometry("550x100")

    # tkinter variable for voltage
    voltage_var = StringVar(Window)
    voltage_var.set("VOLTAGE")
    Voltage = ["120", "208", "240", "277", "480"]
    voltage_dropdown = OptionMenu(Window, voltage_var, *Voltage,
                                  command=lambda x: function(x, phase_var.get(), Window))
    voltage_dropdown.pack(side = "left")

    # tkinter variable for phase
    phase_var = StringVar(Window)
    phase_var.set("PHASE")
    Phase = ["SINGLE PHASE", "THREE PHASE"]
    phase_dropdown = OptionMenu(Window, phase_var, *Phase,
                                command=lambda x: function(voltage_var.get(), x, Window))
    phase_dropdown.pack(side = "left")
    Window.mainloop()

def function(voltage, phrase, Window):
    if voltage == '120' and phrase == "SINGLE PHASE":
        print(True)

edit_or_retrieve()

Note that it is also better to use different variables for different instances of widgets.

  • Related