Daniel Wigmore
Daniel Wigmore

Reputation: 97

issue with python Tkinter drop down menu

The following code is a program to append buttons onto an existing program so selection can occur on a more friendly interface rather than inside the code. I am trying to use drop down menu's but the setEthAnt1 function seems to have an error: TypeError: setEthAnt1() takes no arguments (1 given). I do not know what arg i am failing to pass in. Does anyone have any ideas?

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

Upvotes: 0

Views: 1461

Answers (1)

smont
smont

Reputation: 1348

When you specify a command for an OptionMenu, the value of the selected item will be sent into the command, which essentially makes your var.get() unneeded. See below:

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

If you don't want setEthAnt1 to have any parameters and still use var.get(), you can make the command for the OptionMenu a lamda function like so:

OptionMenu(root, var, "1","2","3","4","5","6","7","8","9","10", command = lambda _: setEthAnt1).pack()

Upvotes: 3

Related Questions