Python tkinter之ComboBox(下拉框)
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()
运行
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)