วิดเจ็ต Tkinter Combobox เป็นหนึ่งในวิดเจ็ตที่มีประโยชน์ในการปรับใช้เมนูดรอปดาวน์ในแอปพลิเคชัน มันใช้การรวมกันของวิดเจ็ตรายการและวิดเจ็ตกล่องรายการที่ด้านบน เราสามารถเลือกรายการเมนูโดยพิมพ์ชื่อรายการ (ถ้ามีอยู่ในรายการเมนู) ในช่องรายการ อย่างไรก็ตาม บางครั้ง อาจมีบางกรณีที่เราต้องการใช้การเติมข้อความอัตโนมัติเพื่อเลือกรายการเมนู
ในการสร้าง Combobox ที่เติมข้อความอัตโนมัติ เราจะสร้างกล่องรายการก่อนเพื่อแสดงรายการเมนูและวิดเจ็ตรายการเพื่อแสดงเมนูที่เลือก คุณสามารถผูกเหตุการณ์ "Keyrelease" กับวิดเจ็ตรายการเพื่อค้นหาคีย์เวิร์ดเฉพาะในรายการ หากมีรายการอยู่ เราจะอัปเดตวิดเจ็ตกล่องรายการ
ตัวอย่าง
ในตัวอย่างนี้ เราจะสร้างสองฟังก์ชันดังกล่าว
- ฟังก์ชัน check(e) จะพบว่ารายการที่ป้อนมีอยู่ในรายการหรือไม่ หากรายการตรงกับคีย์เวิร์ดที่ป้อน เราจะอัปเดตวิดเจ็ตรายการโดยแทรกข้อมูลเฉพาะ
- ฟังก์ชัน อัปเดต (ข้อมูล) จะอัปเดตช่องรายการโดยใส่ค่าลงในวิดเจ็ตรายการ
# Import the Required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame or window
win= Tk()
# Set the size of the window
win.geometry("700x350")
# Set the title of the window
win.title("Combobox- TutorialsPoint")
# Update the Entry widget with the selected item in list
def check(e):
v= entry.get()
if v=='':
data= values
else:
data=[]
for item in values:
if v.lower() in item.lower():
data.append(item)
update(data)
def update(data):
# Clear the Combobox
menu.delete(0, END)
# Add values to the combobox
for value in data:
menu.insert(END,value)
# Add a Label widget
label= Label(win, text= "Demo Combobox Widget", font= ('Helvetica 15
bold'), background= "green3")
label.pack(padx= 10, pady= 25)
# Add a Bottom Label
text= Label(win, text="Select a Programming Language")
text.pack(padx= 15,pady= 20)
# Create an Entry widget
entry= Entry(win, width= 35)
entry.pack()
entry.bind('<KeyRelease>',check)
# Create a Listbox widget to display the list of items
menu= Listbox(win)
menu.pack()
# Create a list of all the menu items
values= ['Python', 'C++', 'Java','Ruby on Rails', 'Rust',
'GoLang','Objective-C', 'C# ', 'PHP', 'Swift', 'JavaScript']
# Add values to our combobox
update(values)
# Binding the combobox onclick
win.mainloop() ผลลัพธ์
การเรียกใช้สคริปต์ Python ด้านบนจะแสดงหน้าต่างพร้อมวิดเจ็ตรายการและกล่องรายการ เมื่อใดก็ตามที่เราป้อนคำหลัก มันจะอัปเดตวิดเจ็ตกล่องรายการซึ่งแสดงผลลัพธ์ที่ตรงกับคำหลักที่ป้อน
