啟用 Tkinter Combobox 的多選功能
Tkinter 是一個功能強大且流行的 GUI 工具包,廣泛用於在 Python 中建立桌面應用程式。任何 GUI 應用程式的關鍵元件之一是從列表中選擇值的能力。Tkinter 為此提供了各種小部件,包括組合框,它是文字欄位和下拉列表的組合。
預設情況下,Tkinter 中的組合框只允許從下拉列表中選擇一個值。但是,在某些情況下,使用者可能需要從列表中選擇多個值。這可以透過啟用組合框的多選功能在 Tkinter 中實現。
在本文中,我們將討論兩種實現從 tkinter 組合框中選擇多個值的方法。
使用 Listbox 小部件
啟用 tkinter 組合框中的多選的最簡單方法是使用 Listbox 小部件。在這種方法中,我們建立一個 Listbox 小部件並將其新增到組合框中。Listbox 小部件將顯示下拉列表,使用者可以透過單擊值從其中選擇多個值。
示例
以下是一個演示此方法的示例程式碼:
import tkinter as tk
from tkinter import ttk
# create a tkinter window
window = tk.Tk()
window.geometry("720x250")
window.title("Multiple Selection Combobox using the Listbox widget")
# define the values for the dropdown list
values = ["Value 1", "Value 2", "Value 3", "Value 4", "Value 5"]
# create a label for the combobox
label = ttk.Label(window, text="Select values:")
# create a combobox
combobox = ttk.Combobox(window, state="readonly")
# create a Listbox widget for the dropdown list
listbox = tk.Listbox(window, selectmode="multiple", exportselection=0)
for value in values:
listbox.insert(tk.END, value)
# define a function to update the combobox when the user selects or deselects a value
def update_combobox():
# Get selected values from the Listbox widget
selected_values = [listbox.get(idx) for idx in listbox.curselection()]
# Update the combobox with the selected values
combobox.configure(width=40, height=7)
combobox.set(", ".join(selected_values))
# bind the update_combobox function to the Listbox widget
listbox.bind("<<ListboxSelect>>", lambda _: update_combobox())
# pack the label, combobox, and Listbox widget
label.pack(side="top", anchor="w", pady=30)
combobox.pack(side="top", pady=30)
listbox.pack(side="top")
# start the main loop
window.mainloop()
輸出

在上述實現中,我們建立了一個 **tkinter** 視窗、一個用於 **組合框** 的標籤和一個 **組合框**。我們定義下拉列表的值並建立一個 Listbox 小部件來顯示這些值。
我們將 **Listbox** 小部件的 **selectmode** 屬性設定為“multiple”以允許使用者選擇多個值。我們使用 **insert()** 方法將值插入到 **Listbox** 小部件中。
我們還將 **Listbox** 小部件的 **exportselection** 屬性設定為 0,以防止當用戶單擊 **Listbox** 外部時取消選擇先前選擇的專案。
為了在 **組合框** 中顯示選定的值,我們定義了一個函式 **update_combobox()**,該函式從 **Listbox** 小部件中檢索選定的值並相應地更新 **組合框** 的文字欄位。當用戶在 **Listbox** 小部件中選擇或取消選擇值時,將呼叫此函式。
我們將 **<<ListboxSelect>>** 事件繫結到一個匿名函式,該函式呼叫 **update_combobox()** 函式,以確保每當使用者進行選擇時 **組合框** 都被更新。
當用戶從下拉列表中選擇多個值時,它們將以逗號分隔的形式顯示在 **組合框** 的文字欄位中。
使用 Checkbutton 小部件
啟用 tkinter 組合框中的多選的另一種方法是使用 Checkbutton 小部件。在這種方法中,我們為下拉列表中的每個值建立一個 Checkbutton 小部件並將其新增到組合框中。使用者可以透過單擊相應的 Checkbutton 來選擇多個值。
示例
以下是一個演示此方法的示例程式碼:
import tkinter as tk
from tkinter import ttk
# create a tkinter window
window = tk.Tk()
window.geometry("720x250")
window.title("Multiple Selection Combobox using the Checkbox Widget")
# define the values for the dropdown list
values = ["Value 1", "Value 2", "Value 3", "Value 4", "Value 5"]
# create a label for the combobox
label = ttk.Label(window, text="Select values:")
# create a combobox
combobox = ttk.Combobox(window)
# create a list of BooleanVar objects to hold the state of the Checkbuttons
checkbuttons_vars = [tk.BooleanVar() for value in values]
# create a Checkbutton widget for each value in the dropdown list
checkbuttons = []
for index, value in enumerate(values):
checkbutton = ttk.Checkbutton(window, text=value, variable=checkbuttons_vars[index])
checkbutton.pack(side="top", anchor="w")
checkbuttons.append(checkbutton)
# define a function to update the combobox when the user selects or deselects a value
def update_combobox():
selected_values = [value for value, var in zip(values, checkbuttons_vars) if var.get()]
combobox.configure(width=40, height=7)
combobox.delete(0, tk.END)
combobox.insert(0, ", ".join(selected_values))
# add a button to update the combobox
update_button = ttk.Button(window, text="Update", command=update_combobox)
update_button.pack(side="bottom")
# pack the label and combobox
label.pack(side="top", anchor="w", pady=30)
combobox.pack(side="top")
# start the main loop
window.mainloop()
輸出

總的來說,這種方法為從 tkinter 組合框中選擇多個值提供了更直觀的使用者介面,但需要更多程式碼來建立 Checkbutton 並更新組合框。
結論
總之,啟用從 tkinter 組合框中選擇多個值可以透過多種方式實現。我們在本文中討論了兩種方法:使用 Listbox 小部件和使用 Checkbutton 小部件。
使用 Listbox 小部件時,使用者可以透過單擊值來選擇多個值。這種方法簡單且程式碼量少,但對使用者來說可能不那麼直觀。
使用 Checkbutton 小部件時,使用者可以透過單擊相應的 Checkbutton 來選擇多個值。這種方法提供了更直觀的使用者介面,但需要更多程式碼來建立 Checkbutton 並更新組合框。
總的來說,在 tkinter 組合框中啟用多選是一個有用的功能,可以增強 GUI 應用程式的功能和可用性。
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP