12

"I want to populate option menus in Tkinter with items from various lists, how do i do that? In the code below it treats the entire list as one item in the menu. I tried to use a for statement to loop through the list but it only gave me the value 'a' several times.

from Tkinter import *

def print_it(event):
  print var.get()

root = Tk()
var = StringVar()
var.set("a")
lst = ["a,b,c,d,e,f"]
OptionMenu(root, var, lst, command=print_it).pack()
root.mainloop()

I want to now pass the variable to this function, but i'm getting a syntax error for the second line:

def set_wkspc(event):
  x = var.get()
  if x = "Done":
      break
  else:
      arcpy.env.workspace = x
  print x
kflaw
  • 424
  • 1
  • 10
  • 26

2 Answers2

20

lst in your code is a list with a single string.

Use a list with multiple menu names, and specify them as follow:

....
lst = ["a","b","c","d","e","f"]
OptionMenu(root, var, *lst, command=print_it).pack()
....
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • my next question is, is there a way to choose more than one option? that I would then populate a new list with to perform an action on? and, can i the user input options as well, vs. a drop down list? – kflaw Aug 13 '13 at 15:40
  • @kflaw, No. OptionMenu allow only one item selection. You may want use Listbox. – falsetru Aug 13 '13 at 15:44
  • thanks i will look at that. i am modifying the function that i want to pass the variable from option menu to, but it doesn't like the syntax of the second line in the function and i'm not sure how to fix? i've posted above – kflaw Aug 13 '13 at 16:59
  • Use `==` instead of `=`. Please post a separated question for other question. – falsetru Aug 13 '13 at 17:07
1

In your code in line 2 you used = instead use == for your if statement and don't use the break keyword outside a loop instead use pass. Change it to the following:

if x == "Done":
    pass
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Aditya
  • 11
  • 1