簡體   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