运行 个命令基于 OptionMenu 的选择
Running commands based on OptionMenu's selection
我如何调整它,以便如果从菜单中选择 xyz17
,函数 xyz17()
是 运行?
我知道 command = xyz17
但我不确定如何使其动态变化以使其取决于菜单选择。
from tkinter import *
def xyz17():
print('xyz17')
def abc27():
print('abc27')
def qwe90():
print('qwe90')
def uio19():
print('uio19')
def jkl09():
print('jkl09')
def zxc28():
print('zxc28')
class Menu(OptionMenu):
def __init__(self, master, status, *options):
self.var = StringVar(master)
self.var.set(status)
OptionMenu.__init__(self, master, self.var, *options)
def main():
TopFrame = Frame(root)
TopFrame.pack()
Menu1 = Menu(TopFrame, 'xyz', 'xyz17','abc27','qwe90')
Menu2 = Menu(TopFrame, 'uio', 'uio19','jkl09','zxc28')
Menu1.pack()
Menu2.pack()
root = Tk()
main()
root.mainloop()
请注意,每个打印值的函数仅作为示例,我希望代码 运行 函数本身。我知道这一点:
class Menu(OptionMenu):
def __init__(self, master, status, *options):
self.var = StringVar(master)
self.var.set(status)
OptionMenu.__init__(self, master, self.var, *options, command=self.func)
def func(self,value):
print (value)
但是,这与我的场景无关,因为它只是获取值并打印它,我希望它实际上 运行 函数本身。
如果你想运行选项的特定方法,只需检查发送的字符串和select基于字符串的方法使用if
/ elif
语句:
from tkinter import *
def xyz17():
print('xyz17')
def abc27():
print('abc27')
def qwe90():
print('qwe90')
def uio19():
print('uio19')
def jkl09():
print('jkl09')
def zxc28():
print('zxc28')
class Menu(OptionMenu):
def __init__(self, master, status, *options):
self.var = StringVar(master)
self.var.set(status)
OptionMenu.__init__(self, master, self.var, *options, command=self.option_handle)
def option_handle(self, selected):
# above specific case is simply print(selected) but
if selected == "xyz17":
xyz17()
elif selected == "abc27":
abc27()
elif selected == "qwe90":
qwe90()
elif selected == "uio19":
uio19()
elif selected == "jkl09":
jkl09()
elif selected == "zxc28":
zxc28()
# if you specifically want to call methods that has exactly
# the same name as options
# eval(selected + "()")
def main():
TopFrame = Frame(root)
TopFrame.pack()
Menu1 = Menu(TopFrame, 'xyz', 'xyz17','abc27','qwe90')
Menu2 = Menu(TopFrame, 'uio', 'uio19','jkl09','zxc28')
Menu1.pack()
Menu2.pack()
root = Tk()
main()
root.mainloop()
运行 基于选项菜单选择的命令的一种方法是使用函数字典:func_dict = {option: function, ...}
然后将以下函数传递给 command
选项OptionMenu
:
def func(value):
func_dict[value]()
执行所选选项对应的功能。
这是一个例子:
from tkinter import *
options = ['xyz', 'xyz17', 'abc27', 'qwe90', 'uio', 'uio19', 'jkl09', 'zxc28']
func_dict = {option: lambda opt=option: print(opt) for option in options}
class Menu(OptionMenu):
def __init__(self, master, status, *options):
self.var = StringVar(master)
self.var.set(status)
OptionMenu.__init__(self, master, self.var, *options, command=self.func)
def func(self, option):
func_dict[option]()
def main():
topFrame = Frame(root)
topFrame.pack()
menu1 = Menu(topFrame, 'xyz', 'xyz17','abc27','qwe90')
menu2 = Menu(topFrame, 'uio', 'uio19','jkl09','zxc28')
menu1.pack()
menu2.pack()
root = Tk()
main()
root.mainloop()
我如何调整它,以便如果从菜单中选择 xyz17
,函数 xyz17()
是 运行?
我知道 command = xyz17
但我不确定如何使其动态变化以使其取决于菜单选择。
from tkinter import *
def xyz17():
print('xyz17')
def abc27():
print('abc27')
def qwe90():
print('qwe90')
def uio19():
print('uio19')
def jkl09():
print('jkl09')
def zxc28():
print('zxc28')
class Menu(OptionMenu):
def __init__(self, master, status, *options):
self.var = StringVar(master)
self.var.set(status)
OptionMenu.__init__(self, master, self.var, *options)
def main():
TopFrame = Frame(root)
TopFrame.pack()
Menu1 = Menu(TopFrame, 'xyz', 'xyz17','abc27','qwe90')
Menu2 = Menu(TopFrame, 'uio', 'uio19','jkl09','zxc28')
Menu1.pack()
Menu2.pack()
root = Tk()
main()
root.mainloop()
请注意,每个打印值的函数仅作为示例,我希望代码 运行 函数本身。我知道这一点:
class Menu(OptionMenu):
def __init__(self, master, status, *options):
self.var = StringVar(master)
self.var.set(status)
OptionMenu.__init__(self, master, self.var, *options, command=self.func)
def func(self,value):
print (value)
但是,这与我的场景无关,因为它只是获取值并打印它,我希望它实际上 运行 函数本身。
如果你想运行选项的特定方法,只需检查发送的字符串和select基于字符串的方法使用if
/ elif
语句:
from tkinter import *
def xyz17():
print('xyz17')
def abc27():
print('abc27')
def qwe90():
print('qwe90')
def uio19():
print('uio19')
def jkl09():
print('jkl09')
def zxc28():
print('zxc28')
class Menu(OptionMenu):
def __init__(self, master, status, *options):
self.var = StringVar(master)
self.var.set(status)
OptionMenu.__init__(self, master, self.var, *options, command=self.option_handle)
def option_handle(self, selected):
# above specific case is simply print(selected) but
if selected == "xyz17":
xyz17()
elif selected == "abc27":
abc27()
elif selected == "qwe90":
qwe90()
elif selected == "uio19":
uio19()
elif selected == "jkl09":
jkl09()
elif selected == "zxc28":
zxc28()
# if you specifically want to call methods that has exactly
# the same name as options
# eval(selected + "()")
def main():
TopFrame = Frame(root)
TopFrame.pack()
Menu1 = Menu(TopFrame, 'xyz', 'xyz17','abc27','qwe90')
Menu2 = Menu(TopFrame, 'uio', 'uio19','jkl09','zxc28')
Menu1.pack()
Menu2.pack()
root = Tk()
main()
root.mainloop()
运行 基于选项菜单选择的命令的一种方法是使用函数字典:func_dict = {option: function, ...}
然后将以下函数传递给 command
选项OptionMenu
:
def func(value):
func_dict[value]()
执行所选选项对应的功能。
这是一个例子:
from tkinter import *
options = ['xyz', 'xyz17', 'abc27', 'qwe90', 'uio', 'uio19', 'jkl09', 'zxc28']
func_dict = {option: lambda opt=option: print(opt) for option in options}
class Menu(OptionMenu):
def __init__(self, master, status, *options):
self.var = StringVar(master)
self.var.set(status)
OptionMenu.__init__(self, master, self.var, *options, command=self.func)
def func(self, option):
func_dict[option]()
def main():
topFrame = Frame(root)
topFrame.pack()
menu1 = Menu(topFrame, 'xyz', 'xyz17','abc27','qwe90')
menu2 = Menu(topFrame, 'uio', 'uio19','jkl09','zxc28')
menu1.pack()
menu2.pack()
root = Tk()
main()
root.mainloop()