How do populate a Tkinter optionMenu with items in a list

大城市里の小女人 提交于 2019-12-23 08:26:14

问题


"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

回答1:


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


来源:https://stackoverflow.com/questions/18212645/how-do-populate-a-tkinter-optionmenu-with-items-in-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!