【问题标题】:Python 3.5: Assigning multiple variables using choices in TKInter dropdown menuPython 3.5:使用 TKInter 下拉菜单中的选项分配多个变量
【发布时间】:2017-05-28 08:11:24
【问题描述】:

感谢 furas,我有以下 ListBox 代码:

import tkinter as tk

def on_button():
#     for i, var in enumerate(o_vars):
#         print('OptionMenu {}: {}'.format(i, var.get()))
#     print()

    print('ListBox:', l.curselection())
    for i in l.curselection():
        print('option:', OPTIONS[i])
    print()


# --- main ---

OPTIONS = ["Script 1","Script 2","Script 3","Script 4","Script 5"]

root = tk.Tk()

# --- Listbox ---

tk.Label(root, text='Listbox', bg='#aaa').pack(fill='x')

l = tk.Listbox(root, selectmode='multiple')
l.pack()
l.insert('end', *OPTIONS)

# --- others ---

b = tk.Button(root, text='OK', command=on_button)
b.pack(fill='x')

root.mainloop()

当我运行它时,它会给我以下弹出窗口(如下图所示)。然后我做出我的选择。

这是我卡住的地方...我想说如果用户选择了 Script2 打印“脚本 2”。如果用户选择了脚本 5,则打印“脚本 5”。

下面是我试过但出错的代码:

if l.curselection() == 'Script1':
    print ('test')
if l.curselection() == 'Script2':
    print ('test2')

TclError: 无效的命令名“.92911768”

另外,如何在“确定”下方添加“退出”按钮?

*非常感谢任何帮助

【问题讨论】:

  • 当您关闭下拉菜单时,此小部件只能显示一个选项 - 这就是您只能选择一个选项的原因。如果您必须同时选择更多选项,请使用Listbox
  • 顺便说一句:我们使用CamelCase 类名称 - 即。 SimpleFormApp 使代码更具可读性 - PEP 8 -- Style Guide for Python Code
  • 永远不要更改问题的文本!!! - 在下方添加新信息。现在你的问题对其他用户毫无用处。
  • 如果您有新问题,请创建新问题!!!
  • 解决问题的旧方法 - 使用print() 查看变量中的内容以及执行的代码部分。所以使用print(l.curselection()) 看看你会得到什么。

标签: python tkinter menu dropdown


【解决方案1】:

OptionMenu关闭下拉菜单后只能显示一个选项 - 所以它不能选择更多选项。

所以你可以使用以下方法之一:

只有多个OptionMenu 才能选择执行脚本的顺序。

示例在一个窗口中显示所有方法。

import tkinter as tk

# --- functions ---

def on_button():
    for i, var in enumerate(o_vars):
        print('OptionMenu {}: {}'.format(i, var.get()))
    print()

    print('ListBox:', l.curselection())
    for i in l.curselection():
        print('option:', OPTIONS[i])
    print()

    print('ChecboxBox:')
    for i, var in enumerate(cb_vars):
        if var.get():
            print('option:', OPTIONS[i])

# --- main ---

OPTIONS = ["Script 1","Script 2","Script 3","Script 4","Script 5"]

root = tk.Tk()

# --- OptionMenu ---

tk.Label(root, text='OptionMenus', bg='#aaa').pack(fill='x')

o_vars = []

for i in range(3):
    var = tk.StringVar(value='- select -')
    o_vars.append(var)
    o = tk.OptionMenu(root, var, *OPTIONS)
    o.pack()

# --- Listbox ---

tk.Label(root, text='Listbox', bg='#aaa').pack(fill='x')

l = tk.Listbox(root, selectmode='multiple')
l.pack()
l.insert('end', *OPTIONS)

# --- Checkbuttons ---

tk.Label(root, text='Checkbuttons', bg='#aaa').pack(fill='x')

cb_vars = []
for x in OPTIONS:
    var = tk.BooleanVar(value=False)
    cb_vars.append(var)
    c = tk.Checkbutton(root, text=x, variable=var)
    c.pack()

# --- others ---

b = tk.Button(root, text='OK', command=on_button)
b.pack(fill='x')

root.mainloop()

结果:

OptionMenu 1: Script 1
OptionMenu 2: Script 3
OptionMenu 3: Script 5

ListBox: (0, 2, 4)
option: Script 1
option: Script 3
option: Script 5

ChecboxBox:
option: Script 1
option: Script 3
option: Script 5

GitHub:furas/python-examples/tkinter/checkbutton-listbox-optionmenu

【讨论】:

  • 谢谢!不过,我对接下来的步骤有点困惑。假设我想使用下拉菜单的 Listbox 选项并选择 Script 1 和 Script 2 .... 我如何编写以下代码:如果选择的选项是 Script 1 打印“脚本 1”,如果选择的选项是脚本2 打印“脚本 2”
  • 您可以使用Listbox 代替OptionMenul.curselection() 为您提供列表框中选定行的数量,您必须使用它从 OPTIONS 获取文本 - 您可以在函数 on_button() 中看到它,结果为 ListBox: (0, 2, 4)
  • 我很抱歉我对 tkinter 和课程很陌生......我不太明白你上面的意思
  • 使用Listbox 而不是OptionMenu。单击此列表框上的元素 - 您可以选择许多元素。然后单击按钮运行函数on_button。在函数内部,您可以使用curselection() 获取列表框上选定元素的索引 - 对于图像上的选择,它会给出值(0, 2, 4)。然后你可以使用这个索引从列表中获取文本OPTIONS - OPTIONS[0] 给出文本Script 1OPTIONS[2] 给出文本Script 3OPTIONS[4] 给出文本Script 5
猜你喜欢
  • 2013-08-25
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
  • 2019-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多