Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do populate a Tkinter optionMenu with items in a list

"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
like image 523
kflaw Avatar asked Aug 13 '13 15:08

kflaw


2 Answers

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()
....
like image 178
falsetru Avatar answered Nov 09 '22 13:11

falsetru


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
like image 30
Aditya Avatar answered Nov 09 '22 13:11

Aditya