Python ttk模块简介与使用示例
目录
- python编程之ttk模块详细介绍与使用教程
- 1. ttk 简介
- 2. ttk 基本使用
- 2.1 导入模块
- 2.2 创建基础窗口
- 3. ttk 常用组件及示例
- 3.1 按钮 (Button)
- 3.2 标签 (Label)
- 3.3 输入框 (Entry)
- 3.4 组合框 (Combobox)
- 3.5 进度条 (Progressbar)
- 3.6 笔记本 (Notebook)
- 3.7 树视图 (Treeview)
- 4. ttk 样式定制
- 4.1 创建样式对象
- 4.2 自定义按钮样式
- 4.3 创建新样式
- 5. 完整示例应用
- 6. 总结
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,python 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",php 12, "bold"), # 字体 padding=10 # 内边距 ) # 创建自定义样式的按钮 custom_button = ttk.Button( root, text="自定义样式按钮", # 应用样式 ) custom_button.pack(pady=10)
4.3 创建新样式
# 定义新样式 Danger.TButton style.configure( "Danger.TButton", js # 新样式名 foreground="red", # 红色文字 font=("Arial", 12, "bold") ) # 使用新样式的按钮 danger_button = ttk.Button( root, text="危险操作", # 应用新样式 ) 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.handroidobbies["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="提交", , 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.inandroidsert("", 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模块用法内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论