Python 更简洁的方式来配置 tkinter 选项菜单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17056211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
More concise way to configure tkinter option menu?
提问by Chris Aung
I am a beginner in python, normally if i want to add option menu to my program, i would do something like this.
我是 python 的初学者,通常如果我想在我的程序中添加选项菜单,我会做这样的事情。
from Tkinter import*
root=Tk()
mylist=['a','b','c']
var=StringVar(root)
var.set("Select status")
mymenu=OptionMenu(root,var,*mylist)
mymenu.pack()
mymenu.config(font=('calibri',(10)),bg='white',width=12)
mymenu['menu'].config(font=('calibri',(10)),bg='white')
root.mainloop()
It works fine but i am wondering if there is any shorter way to achieve the same result as each option menu will take 7 lines of code. I have to create several option menu so i am looking for a proper and shorter way to do it.
它工作正常,但我想知道是否有任何更短的方法来实现相同的结果,因为每个选项菜单都需要 7 行代码。我必须创建几个选项菜单,所以我正在寻找一种适当且更短的方法来做到这一点。
EDIT: Someone pointed out to create function that will generate option menu. So i tried this,
编辑:有人指出创建将生成选项菜单的功能。所以我试过这个,
from Tkinter import*
def Dropmenu(mylist,status):
var=StringVar(root)
var.set(status)
mymenu=OptionMenu(root,var,*mylist)
mymenu.pack(side=LEFT)
mymenu.config(font=('calibri',(10)),bg='white',width=12)
mymenu['menu'].config(font=('calibri',(10)),bg='white')
root=Tk()
Dropmenu(['a','b','c'],'Select')
root.mainloop()
But now, how do i address the "var" so that i can fetch all the values that user chose? According to my example, all the option menu will have the same "var" value, so i have no way to get the choices that user made for different option menu.
但是现在,我如何解决“var”以便我可以获取用户选择的所有值?根据我的示例,所有选项菜单都将具有相同的“var”值,因此我无法获得用户为不同选项菜单所做的选择。
To make things more clear,let's say if i have 2 option menu
为了让事情更清楚,假设我有 2 个选项菜单
Dropmenu(['a','b','c'],'Select')
Dropmenu(['c','d','e'],'Select')
If i use
如果我使用
myvalue=var.get()
Since both option menus have the same var name, how do i fetch the both values?
由于两个选项菜单具有相同的 var 名称,我如何获取这两个值?
采纳答案by A. Rodas
If you are going to create several menus with the same configuration, I would subclass OptionMenu instead of defining a function:
如果您要创建多个具有相同配置的菜单,我会将 OptionMenu 子类化,而不是定义一个函数:
from Tkinter import*
class MyOptionMenu(OptionMenu):
def __init__(self, master, status, *options):
self.var = StringVar(master)
self.var.set(status)
OptionMenu.__init__(self, master, self.var, *options)
self.config(font=('calibri',(10)),bg='white',width=12)
self['menu'].config(font=('calibri',(10)),bg='white')
root = Tk()
mymenu1 = MyOptionMenu(root, 'Select status', 'a','b','c')
mymenu2 = MyOptionMenu(root, 'Select another status', 'd','e','f')
mymenu1.pack()
mymenu2.pack()
root.mainloop()
In my example, I have supposed the only thing that is going to change is the options, but if each instance has its own background color or font, then you only have to add it as an argument to the __init__method.
在我的示例中,我认为唯一要更改的是选项,但是如果每个实例都有自己的背景颜色或字体,那么您只需将其作为参数添加到__init__方法中即可。

