繁体   English   中英

使用 Tkinter 下拉菜单选项作为不同命令的标准

[英]Using Tkinter Drop-Down Menu choices as criteria for different commands

我被分配创建一个我选择的简单 Python 项目,目前我正在开发一个小的 Tkinter GUI 应用程序。 成功编写了负责多个下拉菜单的代码部分后,我想知道如何使用用户选择的选项组合来创建打开指定 window 的命令。 有什么巧妙的方法吗? 以某种方式定义构建按钮命令的 function 中的选项组合是否足够? 非常感谢您对此提出任何建议。

#first drop-down menu
 options1 = [
    '............',
    '............',
    '............'
]
chosen1 = StringVar()
chosen1.set('............')

vyber1 = OptionMenu(whiteboard, chosen1, *options1)
vyber1.place(relx = 0.35, rely = 0.35, relwidth = 0.35, relheight = 0.05)

# second drop-down menu
options2 = [
    '............',
    '............',
    '............',
    '............'
]
chosen2 = StringVar()
chosen2.set('............')

vyber2 = OptionMenu(whiteboard, chosen2, *options2)
vyber2.place(relx = 0.35, rely = 0.46, relwidth = 0.35, relheight = 0.05)

#third drop-down menu
options3 = [
    '............',
    '............',
    '............',
    '............'
]
chosen3 = StringVar()
chosen3.set('............')

vyber3 = OptionMenu(whiteboard, chosen3, *options3)
vyber3.place(relx = 0.35, rely = 0.57, relwidth = 0.35, relheight = 0.05)

在 Python 中执行此操作的“整洁”且有序的方法是通过字典将map的每个选项组合到所需的窗口打开 function。 查找要调用的 function 只需将当前选择组合在一起形成一个字典键,然后使用它查找相应的 function 来调用。

下面是一个可运行的示例:

import tkinter as tk
from tkinter import messagebox

whiteboard = tk.Tk()
whiteboard.geometry('300x300')


#first drop-down menu
options1 = ['1', '2', '3']
chosen1 = tk.StringVar(value=options1[0])

vyber1 = tk.OptionMenu(whiteboard, chosen1, *options1)
vyber1.place(relx=0.35, rely=0.35, relwidth=0.35, relheight=0.10)

# second drop-down menu
options2 = ['A', 'B', 'C', 'D']
chosen2 = tk.StringVar(value=options2[0])

vyber2 = tk.OptionMenu(whiteboard, chosen2, *options2)
vyber2.place(relx=0.35, rely=0.46, relwidth=0.35, relheight=0.10)

#third drop-down menu
options3 = ['Red', 'Green', 'Blue', 'Yellow']
chosen3 = tk.StringVar(value=options3[0])

vyber3 = tk.OptionMenu(whiteboard, chosen3, *options3)
vyber3.place(relx=0.35, rely=0.57, relwidth=0.35, relheight=0.10)

# Window opening functions.
def open_window_1ARed():
    new_win = tk.Toplevel(whiteboard, width=80, height=20)
    new_win.title('1-A-Red window')
    tk.Label(new_win, text='Hello world').pack()

def open_window_1BBlue():
    new_win = tk.Toplevel(whiteboard, width=80, height=20)
    new_win.title('1-B-Blue window')
    tk.Label(new_win, text='Hello world').pack()

def display_error():
    messagebox.showerror('Sorry', "Unsupported combination!")

# Dictionary mapping combinations of choices to functions.
open_windows_commands = {
    '1ARed': open_window_1ARed,
    '1BBlue': open_window_1BBlue,
}

def open_window():
    key = f'{chosen1.get()}{chosen2.get()}{chosen3.get()}'
    open_window_command = open_windows_commands.get(key, display_error)
    open_window_command()

btn = tk.Button(whiteboard, text='Open window', command=open_window)
btn.place(relx=0.35, rely=0.80)

whiteboard.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM