python编程之ttk模块详细介绍与使用教程
1. ttk 简介
ttk (themed tkinter) 是 python 标准库中 tkinter 的一个扩展模块,提供了更加现代化、主题化的 gui 组件。与传统的 tkinter 组件相比,ttk 组件具有以下优势:
- 支持主题切换,外观更加现代化
- 提供更多高级组件(如进度条、笔记本等)
- 跨平台一致性更好
- 样式可定制性更强
2. ttk 基本使用
2.1 导入模块
import tkinter as tk from tkinter import ttk # 导入ttk模块
2.2 创建基础窗口
# 创建主窗口
root = tk.tk()
# 设置窗口标题
root.title("ttk 教程")
# 设置窗口大小(宽x高)
root.geometry("400x300")
3. ttk 常用组件及示例
3.1 按钮 (button)
# 创建ttk按钮
ttk_button = ttk.button(
root, # 父容器
text="点击我", # 按钮显示文本
command=lambda: print("ttk按钮被点击了") # 点击事件处理函数
)
# 使用pack布局放置按钮
ttk_button.pack(pady=10) # pady设置垂直方向外边距
3.2 标签 (label)
# 创建ttk标签
ttk_label = ttk.label(
root, # 父容器
text="这是一个ttk标签", # 标签文本
font=("arial", 12) # 设置字体
)
ttk_label.pack(pady=10)
3.3 输入框 (entry)
# 创建stringvar用于双向绑定输入框内容
entry_var = tk.stringvar()
# 创建ttk输入框
ttk_entry = ttk.entry(
root, # 父容器
textvariable=entry_var, # 绑定变量
width=30 # 设置宽度
)
ttk_entry.pack(pady=10)
# 创建显示输入内容的按钮
show_button = ttk.button(
root,
text="显示输入内容",
command=lambda: print("输入内容:", entry_var.get()) # 获取输入内容
)
show_button.pack(pady=5)3.4 组合框 (combobox)
# 创建ttk组合框
ttk_combobox = ttk.combobox(
root, # 父容器
values=["选项1", "选项2", "选项3"], # 下拉选项
state="readonly" # 设置为只读(不可编辑)
)
ttk_combobox.pack(pady=10)
ttk_combobox.set("请选择") # 设置默认显示文本
# 绑定选择事件
ttk_combobox.bind("<<comboboxselected>>",
lambda e: print("选择了:", ttk_combobox.get()))3.5 进度条 (progressbar)
# 创建进度条变量
progress_var = tk.doublevar()
# 创建ttk进度条
progress = ttk.progressbar(
root, # 父容器
variable=progress_var, # 绑定变量
maximum=100, # 最大值
length=200 # 进度条长度
)
progress.pack(pady=10)
# 创建控制进度条的按钮
start_button = ttk.button(
root,
text="开始进度",
command=lambda: progress.start(10) # 以10ms间隔开始自动增加
)
start_button.pack(side=tk.left, padx=5)
stop_button = ttk.button(
root,
text="停止进度",
command=progress.stop # 停止自动增加
)
stop_button.pack(side=tk.left, padx=5)3.6 笔记本 (notebook)
# 创建ttk笔记本(标签页容器) notebook = ttk.notebook(root) notebook.pack(fill=tk.both, expand=true, padx=5, pady=5) # 填充整个空间 # 创建第一个标签页 frame1 = ttk.frame(notebook) ttk.label(frame1, text="这是标签页1的内容").pack(pady=20) notebook.add(frame1, text="标签页1") # 添加标签页 # 创建第二个标签页 frame2 = ttk.frame(notebook) ttk.label(frame2, text="这是标签页2的内容").pack(pady=20) notebook.add(frame2, text="标签页2")
3.7 树视图 (treeview)
# 创建ttk树视图
tree = ttk.treeview(root, columns=("name", "age"), show="headings")
# 设置列标题
tree.heading("name", text="姓名")
tree.heading("age", text="年龄")
# 设置列宽度
tree.column("name", width=100)
tree.column("age", width=50)
# 插入数据
tree.insert("", tk.end, values=("张三", 25))
tree.insert("", tk.end, values=("李四", 30))
tree.insert("", tk.end, values=("王五", 28))
tree.pack(pady=10)
# 绑定选中事件
tree.bind("<<treeviewselect>>",
lambda e: print("选中了:", tree.item(tree.focus())["values"]))4. ttk 样式定制
ttk 允许自定义组件样式,使其更符合应用需求。
4.1 创建样式对象
# 获取默认样式对象
style = ttk.style()
# 查看当前可用主题
print("可用主题:", style.theme_names())
# 设置主题
style.theme_use("clam") # 使用clam主题4.2 自定义按钮样式
# 配置tbutton样式(所有按钮)
style.configure(
"tbutton", # 样式名称
foreground="blue", # 前景色(文字颜色)
font=("arial", 12, "bold"), # 字体
padding=10 # 内边距
)
# 创建自定义样式的按钮
custom_button = ttk.button(
root,
text="自定义样式按钮",
style="tbutton" # 应用样式
)
custom_button.pack(pady=10)4.3 创建新样式
# 定义新样式 danger.tbutton
style.configure(
"danger.tbutton", # 新样式名
foreground="red", # 红色文字
font=("arial", 12, "bold")
)
# 使用新样式的按钮
danger_button = ttk.button(
root,
text="危险操作",
style="danger.tbutton" # 应用新样式
)
danger_button.pack(pady=10)5. 完整示例应用
下面是一个结合了多个ttk组件的完整示例:
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
class ttkdemoapp:
def __init__(self, root):
self.root = root
self.root.title("ttk 综合示例")
self.root.geometry("500x500")
# 创建样式
self.setup_styles()
# 创建界面
self.create_widgets()
def setup_styles(self):
"""设置自定义样式"""
self.style = ttk.style()
self.style.theme_use("clam")
# 配置默认按钮样式
self.style.configure(
"tbutton",
padding=6,
font=("arial", 10)
)
# 创建成功按钮样式
self.style.configure(
"success.tbutton",
foreground="green",
font=("arial", 10, "bold")
)
def create_widgets(self):
"""创建所有界面组件"""
# 创建笔记本(标签页)
self.notebook = ttk.notebook(self.root)
self.notebook.pack(fill=tk.both, expand=true, padx=5, pady=5)
# 创建第一个标签页 - 表单
self.create_form_tab()
# 创建第二个标签页 - 数据展示
self.create_data_tab()
def create_form_tab(self):
"""创建表单标签页"""
form_frame = ttk.frame(self.notebook)
self.notebook.add(form_frame, text="表单")
# 表单标题
ttk.label(
form_frame,
text="用户注册表单",
font=("arial", 14, "bold")
).pack(pady=10)
# 用户名输入
ttk.label(form_frame, text="用户名:").pack()
self.username = ttk.entry(form_frame, width=30)
self.username.pack(pady=5)
# 密码输入
ttk.label(form_frame, text="密码:").pack()
self.password = ttk.entry(form_frame, width=30, show="*")
self.password.pack(pady=5)
# 性别选择
ttk.label(form_frame, text="性别:").pack()
self.gender = tk.stringvar()
ttk.radiobutton(form_frame, text="男", variable=self.gender, value="male").pack()
ttk.radiobutton(form_frame, text="女", variable=self.gender, value="female").pack()
# 兴趣爱好
ttk.label(form_frame, text="兴趣爱好:").pack()
self.hobbies = {
"reading": tk.booleanvar(),
"sports": tk.booleanvar(),
"music": tk.booleanvar()
}
ttk.checkbutton(form_frame, text="阅读", variable=self.hobbies["reading"]).pack()
ttk.checkbutton(form_frame, text="运动", variable=self.hobbies["sports"]).pack()
ttk.checkbutton(form_frame, text="音乐", variable=self.hobbies["music"]).pack()
# 提交按钮
ttk.button(
form_frame,
text="提交",
style="success.tbutton",
command=self.submit_form
).pack(pady=20)
def create_data_tab(self):
"""创建数据展示标签页"""
data_frame = ttk.frame(self.notebook)
self.notebook.add(data_frame, text="数据")
# 树视图
self.tree = ttk.treeview(
data_frame,
columns=("name", "age", "department"),
show="headings"
)
# 设置列
self.tree.heading("name", text="姓名")
self.tree.heading("age", text="年龄")
self.tree.heading("department", text="部门")
# 设置列宽
self.tree.column("name", width=100)
self.tree.column("age", width=50)
self.tree.column("department", width=150)
self.tree.pack(fill=tk.both, expand=true, padx=5, pady=5)
# 添加一些示例数据
self.add_sample_data()
# 添加控制按钮
control_frame = ttk.frame(data_frame)
control_frame.pack(pady=5)
ttk.button(
control_frame,
text="添加数据",
command=self.add_data
).pack(side=tk.left, padx=5)
ttk.button(
control_frame,
text="删除选中",
command=self.delete_selected
).pack(side=tk.left, padx=5)
def add_sample_data(self):
"""添加示例数据到树视图"""
sample_data = [
("张三", 28, "技术部"),
("李四", 32, "市场部"),
("王五", 25, "人事部")
]
for data in sample_data:
self.tree.insert("", tk.end, values=data)
def add_data(self):
"""添加新数据"""
self.tree.insert("", tk.end, values=("新员工", 0, "未分配"))
def delete_selected(self):
"""删除选中行"""
selected_item = self.tree.selection()
if selected_item:
self.tree.delete(selected_item)
else:
messagebox.showwarning("警告", "请先选择要删除的行")
def submit_form(self):
"""处理表单提交"""
username = self.username.get()
password = self.password.get()
gender = self.gender.get()
hobbies = [hobby for hobby, var in self.hobbies.items() if var.get()]
if not username or not password:
messagebox.showerror("错误", "用户名和密码不能为空")
return
message = f"""
注册信息:
用户名: {username}
性别: {gender if gender else '未选择'}
兴趣爱好: {', '.join(hobbies) if hobbies else '无'}
"""
messagebox.showinfo("注册成功", message)
self.username.delete(0, tk.end)
self.password.delete(0, tk.end)
self.gender.set("")
for var in self.hobbies.values():
var.set(false)
if __name__ == "__main__":
root = tk.tk()
app = ttkdemoapp(root)
root.mainloop()6. 总结
ttk 模块为 python 的 gui 开发提供了更加现代化和灵活的组件,主要特点包括:
- 提供了更多高级组件(如 combobox、notebook、treeview 等)
- 支持主题切换,可以改变整个应用的外观
- 样式可定制性强,可以精细控制组件的外观
- 跨平台表现一致
通过本教程,你应该已经掌握了 ttk 的基本使用方法,包括常用组件的创建、布局、事件绑定以及样式定制。ttk 与传统的 tkinter 组件可以混合使用,但为了保持界面一致性,建议尽可能使用 ttk 组件。
在实际开发中,你可以根据需求选择合适的组件,并通过样式系统来创建符合品牌或设计要求的界面。
到此这篇关于python ttk模块简介与使用示例的文章就介绍到这了,更多相关python ttk模块用法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论