Python GUI 编程:tkinter 初学者入门指南——Ttk 组合框 Combobox

在本文中,将介绍如何创建一个 tkinter Combobox 组合框小部件,该小部件允许用户从一组值中选择一个值。

Combobox 组合框小部件是新增的 Ttk 主题小部件,是 Entry 文本框和 Listbox 列表框的组合。除了允许在一组值中选择一个值外,它还允许输入自定义值。

创建组合框

要创建组合框小部件,使用以下构造函数。

复制代码
current_var = tk.StringVar()
combobox = ttk.Combobox(master, textvariable=current_var)

获取组合框选定的值

textvariable 参数将变量链接到 current_var。要获取当前组合框选定的值,可以使用 current_var 变量。

复制代码
current_value = current_var.get()

或者,直接使用 combobox 对象的 get() 方法:

复制代码
current_value = combobox.get()

设置组合框的值

要设置当前值,可以使用 current_var 变量或 combobox 对象的 set() 方法。

复制代码
current_value.set(new_value)

combobox.set(new_value)

默认情况下,可以直接在组合框中输入值。如果不允许直接输入值,可以将组合框设置为只读 readonly 否则,设置为 normal

复制代码
combobox['state'] = 'readonly'

批量设置组合框的值

可以为组合框分配一个列表或元组,进行批量赋值。

复制代码
combobox['values'] = ('value1', 'value2', 'value3')`

绑定事件

当组合框的值发生更改时,可以触发事件,使用 bind() 方法绑定 <<ComboboxSelected>> 事件。

combobox.bind('<<ComboboxSelected>>', callback)

Combobox 组合框示例

复制代码
import tkinter as tk
from tkinter.messagebox import showinfo
from tkinter import ttk
from calendar import month_name
from datetime import datetime
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Combobox 组合框演示')
def year_changed(event):
    showinfo(title='结果', message=f'你选择了 {selected_year.get()}!'
    )
def month_changed(event):
    showinfo(title='结果', message=f'你选择了 {selected_month.get()}!'
    )

label = tk.Label(text="请选择年份:")
label.pack(fill=tk.X, padx=5, pady=5)
selected_year = tk.StringVar()
combobox1 = ttk.Combobox(root, textvariable=selected_year)
combobox1['values'] = [2023, 2024, 2025]
combobox1['state'] = 'readonly'
combobox1.pack(padx=5, pady=5)
combobox1.bind('<<ComboboxSelected>>', year_changed)

label = tk.Label(text="请选择月份:")
label.pack(fill=tk.X, padx=5, pady=5)
selected_month = tk.StringVar()
combobox2 = ttk.Combobox(root, textvariable=selected_month)
combobox2['values'] = [month_name[m][0:3] for m in range(1, 13)]
combobox2['state'] = 'readonly'
combobox2.pack(padx=5, pady=5)
combobox2.bind('<<ComboboxSelected>>', month_changed)

# 设置当前月份为组合框的当前值
current_month = datetime.now().strftime('%b')
combobox2.set(current_month)
root.mainloop()

组合框联动

复制代码
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Combobox 组合框演示')

label = tk.Label(text="请选择省份:")
label.pack(fill=tk.X, padx=5, pady=5)
combobox1 = ttk.Combobox(root)
combobox1['values'] = ["山东省", "江苏省", "吉林省"]
combobox1['state'] = 'readonly'
combobox1.pack(padx=5, pady=5)

label = tk.Label(text="请选择城市:")
label.pack(fill=tk.X, padx=5, pady=5)
combobox2 = ttk.Combobox(root)
combobox2['state'] = 'readonly'
combobox2.pack(padx=5, pady=5)

# 联动响应
region = {
  '山东省': ["济南", "青岛", "淄博"],
  '江苏省': ["南京", "苏州" ],
  '吉林省': ["长春", "吉林"]
}
def handle(event):
  selected = combobox1.get()
  combobox2['values'] = region[selected]

combobox1.bind('<<ComboboxSelected>>', handle)  
root.mainloop()
相关推荐
Scott9999HH5 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
AI云海6 小时前
python 列表、元组、集合和字典
开发语言·python
二十雨辰6 小时前
[爬虫]-Urllib
爬虫·python
玉鸯8 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
weixin_446260859 小时前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask
我的xiaodoujiao9 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest
qetfw9 小时前
MXU:Tauri 2 + React 的 MaaFramework 跨平台 GUI 源码
前端·python·react.js·前端框架·开源项目·效率工具
用户83562907805110 小时前
Python 实现 Excel 页面布局与打印设置自动化
后端·python
一次旅行10 小时前
Python+大模型端到端自动化日报系统
开发语言·python·自动化