简体   繁体   English

python tk 不同的功能取决于所选的下拉列表项

[英]python tk different functions depending on drop down list item selected

I am trying to call a different function in a drop down list depending on what the user has selected.我试图根据用户选择的内容在下拉列表中调用不同的 function。 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.例如,假设我想要调用 2 个函数,具体取决于是否在下拉列表中选择了 function1 或 function2。

this is the call for tk i used:这是我使用的 tk 调用:

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.无论选择哪个选项,它都会运行相同的 function。

is there a way to assign a function to different options in the option menu?有没有办法将 function 分配给选项菜单中的不同选项?

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()

is there a way to assign a function to different options in the option menu?有没有办法将 function 分配给选项菜单中的不同选项?

Strictly speaking, no.严格来说,没有。 You can only define a single function to an OptionMenu .您只能将单个 function 定义到OptionMenu However, that single function can use a map to determine which function can run.但是,单个 function 可以使用 map 来确定哪个 function 可以运行。

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.当绑定到选项菜单的 function 运行时,它获取选项菜单的值,使用它在字典中查找 function,然后运行该 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.综上所述, OptionMenu只是一个Menubutton和一个 `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.请注意,上面的示例从未更改出现在菜单按钮上的 label。 You can add code to do that if you wish.如果您愿意,可以添加代码来执行此操作。 I left it out to keep the example simple.为了使示例简单,我将其遗漏了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM