1、ComboBox的基礎屬性
# -*- encoding=utf-8 -*- import tkinter from tkinter import * from tkinter import ttk if __name__ == '__main__': win = tkinter.Tk() # 窗口 win.title('南風丶輕語') # 標題 screenwidth = win.winfo_screenwidth() # 屏幕寬度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 600 height = 500 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 value = StringVar() value.set('CCC') values = ['AAA', 'BBB', 'CCC', 'DDD'] combobox = ttk.Combobox( master=win, # 父容器 height=10, # 高度,下拉顯示的條目數量 width=20, # 寬度 state='readonly', # 設置狀態 normal(可選可輸入)、readonly(只可選)、 disabled cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus... font=('', 20), # 字體 textvariable=value, # 通過StringVar設置可改變的值 values=values, # 設置下拉框的選項 ) print(combobox.keys()) # 可以查看支持的參數 combobox.pack() win.mainloop()

2、綁定選中事件
# -*- encoding=utf-8 -*- import tkinter from tkinter import * from tkinter import ttk def choose(event): # 選中事件 print('選中的數據:{}'.format(combobox.get())) print('value的值:{}'.format(value.get())) if __name__ == '__main__': win = tkinter.Tk() # 窗口 win.title('南風丶輕語') # 標題 screenwidth = win.winfo_screenwidth() # 屏幕寬度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 600 height = 500 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 value = StringVar() value.set('CCC') # 默認選中CCC==combobox.current(2) values = ['AAA', 'BBB', 'CCC', 'DDD'] combobox = ttk.Combobox( master=win, # 父容器 height=10, # 高度,下拉顯示的條目數量 width=20, # 寬度 state='normal', # 設置狀態 normal(可選可輸入)、readonly(只可選)、 disabled cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus... font=('', 20), # 字體 textvariable=value, # 通過StringVar設置可改變的值 values=values, # 設置下拉框的選項 ) combobox.bind('<<ComboboxSelected>>', choose) print(combobox.keys()) # 可以查看支持的參數 combobox.pack() win.mainloop()

3、省市聯動(選中第一個下拉框,自動改變第二個下拉框的值)
from tkinter import StringVar from tkinter import Tk from tkinter import ttk def middle_windows(window, width=400, height=500): # 設置窗口居中 screenwidth = window.winfo_screenwidth() # 屏幕寬度 screenheight = window.winfo_screenheight() # 屏幕高度 x = int((screenwidth - width) / 2) # x軸坐標 y = int((screenheight - height) / 2) # y軸坐標 window.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 放置窗口 window.update() # 更新窗口 def choose(event): widget = event.widget # 當前的組件 value = widget.get() # 選中的值 print('value:{}'.format(value)) if value == 'AAA': value2.set('') # 設置默認是空串 combobox2.configure(values=['AAA1', 'AAA2', 'AAA3']) # 重新設置combobox2可下拉的值 elif value == 'BBB': value2.set('') # 設置默認是空串 combobox2.configure(values=['BBB1', 'BBB2', 'BBB3']) # 重新設置combobox2可下拉的值 else: value2.set('') # 設置默認是空串 combobox2.configure(values=[]) # 重新設置combobox2可下拉的值 if __name__ == '__main__': win = Tk() middle_windows(win) values1 = ['', 'AAA', 'BBB'] value1 = StringVar(win) value1.set(values1[0]) combobox1 = ttk.Combobox( master=win, # 父容器 height=10, # 高度,下拉顯示的條目數量 width=20, # 寬度 state='readonly', # 設置狀態 normal(可選可輸入)、readonly(只可選)、 disabled cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus... font=('', 16), # 字體 textvariable=value1, # 通過StringVar設置可改變的值 values=values1, # 設置下拉框的選項 background='pink', ) print(combobox1.keys()) # 可以查看支持的參數 combobox1.bind('<<ComboboxSelected>>', choose) # 綁定選中事件 combobox1.pack(pady=(50, 0)) value2 = StringVar(win) value2.set('') combobox2 = ttk.Combobox( master=win, # 父容器 height=10, # 高度,下拉顯示的條目數量 width=20, # 寬度 state='readonly', # 設置狀態 normal(可選可輸入)、readonly(只可選)、 disabled cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus... font=('', 16), # 字體 textvariable=value2, # 通過StringVar設置可改變的值 ) combobox2.pack(pady=(100, 0)) win.mainloop()
運行

