[英]Python 3.5: Assigning multiple variables using choices in TKInter dropdown menu
Thanks to furas I have the following code for ListBox:感谢 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()
When I run it, it gives me the following pop-up (image shown below).当我运行它时,它给了我以下弹出窗口(如下图所示)。 I then make my selections.
然后我做出我的选择。
This is where I am stuck... I want to say if user selected Script2 print 'script 2'.这就是我被卡住的地方...我想说是否用户选择了 Script2 打印“脚本 2”。 If user selected script 5, print 'script 5'.
如果用户选择了脚本 5,则打印“脚本 5”。
Below is the code I tried but it errored out:下面是我试过但出错的代码:
if l.curselection() == 'Script1':
print ('test')
if l.curselection() == 'Script2':
print ('test2')
TclError: invalid command name ".92911768" TclError:无效的命令名称“.92911768”
Also, how do I add a "Quit" button below "OK"?另外,如何在“确定”下方添加“退出”按钮?
*Any help is greatly appreciated *任何帮助是极大的赞赏
OptionMenu
after closing dropdown menu can display only one option - so it can't select more options.关闭下拉菜单后的
OptionMenu
只能显示一个选项 - 因此它无法选择更多选项。
So you can use one of this method:因此,您可以使用以下方法之一:
Only with many OptionMenu
you can select in which order execute scripts.只有使用许多
OptionMenu
才能选择执行脚本的顺序。
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()
Result:结果:
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 GitHub: furas/python-examples/tkinter/checkbutton-listbox-optionmenu
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.