如何用列表中的项目填充Tkinter optionMenu [英] How do populate a Tkinter optionMenu with items in a list

查看:89
本文介绍了如何用列表中的项目填充Tkinter optionMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用各种列表中的项目填充Tkinter中的选项菜单,我该怎么做?在下面的代码中,它将整个列表视为菜单中的一个项目.我试图使用for语句来遍历列表,但它多次给了我一个'a'值.

"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

推荐答案

lst是一个包含单个字符串的列表.

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

这篇关于如何用列表中的项目填充Tkinter optionMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆