Python 3.5:使用TKInter下拉菜单中的选项分配多个变量 [英] Python 3.5: Assigning multiple variables using choices in TKInter dropdown menu

查看:730
本文介绍了Python 3.5:使用TKInter下拉菜单中的选项分配多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢furas,我为ListBox提供了以下代码:

Thanks to furas I have the following code for 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()

当我运行它时,它会显示以下弹出窗口(如下图所示).然后,我进行选择.

When I run it, it gives me the following pop-up (image shown below). I then make my selections.

这就是我遇到的问题...我想说的是,如果用户选择Script2打印'script 2'.如果用户选择了脚本5,则打印脚本5".

This is where I am stuck... I want to say if user selected Script2 print 'script 2'. If user selected script 5, print 'script 5'.

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

Below is the code I tried but it errored out:

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

TclError:无效的命令名称".92911768"

TclError: invalid command name ".92911768"

此外,如何在确定"下方添加退出"按钮?

Also, how do I add a "Quit" button below "OK"?

*非常感谢您的帮助

推荐答案

OptionMenu关闭下拉菜单后只能显示一个选项-因此无法选择更多选项.

OptionMenu after closing dropdown menu can display only one option - so it can't select more options.

因此您可以使用以下一种方法:

So you can use one of this method:

  • many OptionMenu
  • Listbox which can select many elements
  • many Checkbutton

只有许多OptionMenu,您才能选择执行脚本的顺序.

Only with many OptionMenu you can select in which order execute scripts.

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

Example shows all menthods in one window.

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

这篇关于Python 3.5:使用TKInter下拉菜单中的选项分配多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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