Tkinter:基于下拉菜单中的用户选择编写if语句(Python)

Tkinter:基于下拉菜单中的用户选择编写if语句(Python),python,forms,if-statement,input,tkinter,Python,Forms,If Statement,Input,Tkinter,我有以下代码,它使用TKinter创建了一个弹出框: 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():

我有以下代码,它使用TKinter创建了一个弹出框:

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()
我在列表框中选择脚本2和脚本5

我正在努力编写代码,以实现以下功能:

a) 如果我在列表框中选择了脚本2,我想打印“running Script2” b) 如果我在列表框中选择了脚本5,我想打印“running Script5”

下面是我尝试过的代码,由它出错:

if l.curselection() == OPTIONS[1]:
    print ('running script 2')
if l.curselection() == OPTIONS[4]:
    print ('running script 5')
错误附在下面:

当我尝试使用以下打印语句排除故障时,我得到以下错误:

print(l.curselection())

*非常感谢您的帮助。

l.curselection()
使用
(0,2,4)
等索引重新返回元组。要获取字符串,必须将其与
选项一起使用

for idx in l.curselection():

    if OPTIONS[idx] == "Script 1":
        print('Run script number 1')
    elif OPTIONS[idx] == "Script 2":
        print('Run script number 2')
    # ...
也可以直接使用索引

for idx in l.curselection():

    if idx == 0:
        print('Run script number 1')
    elif idx == 1:
        print('Run script number 2')
    # ...
您甚至可以将脚本按与选项中的文本相同的顺序保留在列表中

scripts = [
    'first-script.py', 'second-script.py', 'third-script.py',
]

for idx in l.curselection():
    print('Run script:', scripts[idx])

编辑:使用不同示例的代码如何使用
l.curseelection()中的信息


您何时运行此代码:
如果l.curseelection()==OPTIONS[1]:
?错误消息表示您在根窗口被销毁后正在执行此操作。“无效命令名。87326560”表示小部件已被销毁。感谢您的快速反馈。你说得对……我确实在运行if l.curselection()==OPTIONS[1]:代码后销毁了小部件。这一次,我没有破坏小部件。。。但它一直在运行,根本不打印任何内容。想法?l.curse()并没有输出你的想法。添加else语句将显示if语句为false。作为建议,为什么不将这些打印语句添加到on_按钮()。可能类似于print(“running%s”%OPTIONS[i])@furas:当我尝试您以前建议的print语句时:“print(l.curselection())”它一直在运行,没有打印出来……它成功了!非常感谢你。从昨晚开始,我一直在帮助我解决这个问题,我无法表达我的感激之情。非常感谢。
import tkinter as tk

# --- functions ---

def on_button():

    # different examples with `curselection()`

    for idx in l.curselection():
        if OPTIONS[idx] == 'Script 1':
           print('Running first script')
        elif OPTIONS[idx] == 'Script 2':
           print('Running second script')
        elif OPTIONS[idx] == 'Script 3':
           print('Running third script')


    for idx in l.curselection():
        if idx == 0:
           print('Running first script')
        elif idx == 1:
           print('Running second script')
        elif idx == 2:
           print('Running third script')

    for idx in l.curselection():
           print('Running script:', OPTIONS[idx])

    for idx in l.curselection():
           print('Running script:', scripts[idx])

# --- main ---

scripts = ["first.py", "second.py", "third.py"]

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

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