0

以下代码是将按钮附加到现有程序的程序,因此可以在更友好的界面上而不是在代码内部进行选择。我正在尝试使用下拉菜单,但 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() 
4

1 回答 1

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像这样为 a lamda 函数创建命令:

OptionMenu(root, var, "1","2","3","4","5","6","7","8","9","10", command = lambda _: setEthAnt1).pack()
于 2012-06-22T15:18:44.143 回答