I am trying to call a different function in a drop down list depending on what the user has selected. For example, say i want to have 2 functions that are called depending on if function1 or function2 is chosen in a drop down list.
this is the call for tk i used:
from TK import *
This is how i write option menus:
Dropdown = OptionMenu("function1","function2",command = run_a_function)
this runs the same function no matter which option is chosen.
is there a way to assign a function to different options in the option menu?
CodePudding user response:
Edit: Added two functions.
Is this what you wanted?
import tkinter as tk
root = tk.Tk()
a = tk.StringVar()
a.set("default")
var = tk.StringVar(root)
var.set("Select")
def _this_what_you_wanted():
print(f'Is this what you wanted?')
def _this_is_second_function():
print(f'this is second function')
def run_a_function(x):
if x == "function1":
_this_what_you_wanted()
else:
a.set("function2")
_this_is_second_function()
opt = tk.OptionMenu(root, var, "function1","function2", command = run_a_function)
opt.pack()
root.mainloop()
CodePudding user response:
is there a way to assign a function to different options in the option menu?
Strictly speaking, no. You can only define a single function to an OptionMenu
. However, that single function can use a map to determine which function can run.
Here's an example that creates a dictionary with functions and the names that should appear in the optionmenu. When the function tied to the optionmenu runs, it gets the value of the optionmenu, uses that to lookup the function in the dictionary, then runs that function.
import tkinter as tk
def func1():
label.configure(text="func1 called")
def func2():
label.configure(text="func2 called")
functions = {
"function 1": func1,
"function 2": func2,
}
def run_a_function(*args):
func_name = function_var.get()
func = functions.get(func_name, None)
func()
root = tk.Tk()
root.geometry("400x200")
function_var = tk.StringVar(value="Choose a function")
dropdown = tk.OptionMenu(root, function_var, *functions.keys(), command=run_a_function)
dropdown.configure(width=15)
label = tk.Label(root, text="")
dropdown.pack(side = "top")
label.pack(side="top", fill="x", pady=10)
root.mainloop()
All of that being said, an OptionMenu
is just a Menubutton
and a `Menu. You can create your own widgets and directly assign functions to the items in the dropdown list.
Here's an example:
import tkinter as tk
def func1():
label.configure(text="func1 called")
def func2():
label.configure(text="func2 called")
root = tk.Tk()
root.geometry("400x200")
dropdown = tk.Menubutton(root, text="Choose a function", indicatoron=True, width=15)
dropdown_menu = tk.Menu(dropdown)
dropdown.configure(menu=dropdown_menu)
dropdown_menu.add_command(label="function 1", command=func1)
dropdown_menu.add_command(label="function 2", command=func2)
label = tk.Label(root, text="")
dropdown.pack(side = "top")
label.pack(side="top", fill="x", pady=10)
root.mainloop()
Note that the above example never changes the label that appears on the menu button. You can add code to do that if you wish. I left it out to keep the example simple.