以下代码是一个将按钮附加到现有程序的程序,因此可以在更友好的界面上而不是在代码内部进行选择。我试图使用下拉菜单,但setEthAnt1函数似乎有一个错误:TypeError:setEthAnt1()不带参数(给定1)。我不知道什么是我无法传递的。有没有人有任何想法?
from Tkinter import *
import ThreegroupsGraphics as three
def run():
three.main()
def setEthAnt1():
name = var.get()
print name
three.OneTo2Ant = name
print three.OneTo2Ant
root = Tk()
var = StringVar()
var.set("Group 1 Ethnic Antagonism")
OptionMenu(root, var, "1","2","3","4","5","6","7","8","9","10", command = setEthAnt1).pack()
butn = Button(root, text = 'run', command = run)
butn.pack()
root.mainloop()
答案 0 :(得分:3)
当您为OptionMenu
指定命令时,所选项目的值将被发送到命令中,这实际上使您的var.get()不需要。见下文:
from Tkinter import *
import ThreegroupsGraphics as three
def run():
three.main()
def setEthAnt1(name):
print name
three.OneTo2Ant = name
print three.OneTo2Ant
root = Tk()
var = StringVar()
var.set("Group 1 Ethnic Antagonism")
OptionMenu(root, var, "1","2","3","4","5","6","7","8","9","10", command = setEthAnt1).pack()
butn = Button(root, text = 'run', command = run)
butn.pack()
root.mainloop()
如果您不希望setEthAnt1
拥有任何参数并且仍然使用var.get()
,您可以像OptionMenu
那样为OptionMenu(root, var, "1","2","3","4","5","6","7","8","9","10", command = lambda _: setEthAnt1).pack()
lamda函数创建命令:
{{1}}