0

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

1 Answer 1

3

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.