赞
踩
ttk.Treeview 是 Tkinter 模块中的一个组件,它提供了一个可用于显示层次结构数据的树状列表。它具有以下特点和功能:
ttk.Treeview 是 Tkinter 中非常强大和灵活的组件,可用于创建各种复杂的层次结构列表和数据展示界面。你可以根据自己的需求来配置和使用 ttk.Treeview,以实现定制的树状列表功能。
import tkinter as tk from tkinter import ttk class GUI: def __init__(self): self.root = tk.Tk() self.root.title('演示窗口') self.root.geometry("600x380+1100+150") self.interface() def interface(self): """"界面编写位置""" pass if __name__ == '__main__': a = GUI() a.root.mainloop()
def interface(self): """"界面编写位置""" # (1)创建样式 style = ttk.Style() style.configure("Treeview", rowheight=20) # 设置行高 style.configure("Treeview.Heading", font=('Arial', 11, 'bold')) # 设置表头字体 # (2)创建 Treeview 控件,设置高度为10行 self.tree = ttk.Treeview(self.root, height=10, style="Treeview") self.tree.grid(row=0, column=0, rowspan=10, columnspan=10, padx=60) # (3)定义列名 self.tree["columns"] = ("Name", "Age", "City") # (4)设置列的标题名称,anchor可设置对其方式:居中(center)/左对齐(w)/右对齐(e),无anchor参数时,标题名称默认居中 self.tree.heading("#0", text="序号", anchor="w") self.tree.heading("Name", text="姓名") self.tree.heading("Age", text="年龄") self.tree.heading("City", text="城市") # (5)设置列宽度(像素),无anchor参数时,列表中的数据除(#0)外其余都是默认左对齐 self.tree.column("#0", width=50) self.tree.column("Name", width=100, anchor="center") self.tree.column("Age", width=90, anchor="center") self.tree.column("City", width=150, anchor="center") # (6)插入默认数据 self.tree.insert("", tk.END, text="1", values=("张三", "25", "深圳"))
解释:
def interface(self): """"界面编写位置""" # (1)创建样式...... # (2)创建 Treeview 控件...... # (3)定义列名...... # (4)设置列的标题名称...... # (5)设置列宽度(像素)...... # (6)插入默认数据 self.tree.insert("", tk.END, text="1", values=("张三", "25", "深圳")) self.tree.insert("", tk.END, text="2", values=("王五", "30", "上海")) # 创建按钮 self.Button0 = tk.Button(self.root, text="获取数据", command=self.get_data) self.Button0.grid(row=11, column=0, ipadx=10) self.Button1 = tk.Button(self.root, text="获取全部数据", command=self.get_whole_data) self.Button1.grid(row=11, column=2, ipadx=10) # 添加文本框 self.Label0 = tk.Label(self.root, text="打印日志:") self.Label0.grid(row=12, column=0, padx=60) self.w1 = tk.Text(self.root, width=50, height=5) self.w1.grid(row=13, column=0, columnspan=5, padx=60) def get_data(self): '''获取选中数据''' selected_items = self.tree.selection() if selected_items: for item in selected_items: row_data = self.tree.item(item)['values'] self.w1.insert("1.0", f"Selected Row Data:{row_data}\n") else: self.w1.insert("1.0", "未选中数据\n") def get_whole_data(self): '''获取全部数据''' rows = self.tree.get_children() # 获取所有的行ID # 遍历每一行 for row in rows: # 获取该行的数据字典 data = self.tree.item(row) self.w1.insert("1.0", f"{data['values']}\n")
import tkinter as tk from tkinter import ttk from tkinter import messagebox class GUI: def __init__(self): self.root = tk.Tk() self.root.title('演示窗口') self.root.geometry("600x380+1100+150") self.interface() def interface(self): """"界面编写位置""" # (1)创建 Treeview 控件, 设置高度为10行 self.tree = ttk.Treeview(self.root, height=10) self.tree.grid(rowspan=10, columnspan=10, padx=30) # (2)定义列名 self.tree["columns"] = ("Name", "Age", "City") # (3)设置列的标题名称 self.tree.heading("#0", text="序号", anchor="w") self.tree.heading("Name", text="姓名") self.tree.heading("Age", text="年龄") self.tree.heading("City", text="城市") # (4)设置列宽度(像素) self.tree.column("#0", width=50) self.tree.column("Name", width=100, anchor="center") self.tree.column("Age", width=90, anchor="center") self.tree.column("City", width=150, anchor="center") # (5)插入默认数据 self.tree.insert("", tk.END, text="1", values=("张三", "25", "深圳")) # 创建按钮 self.Button0 = tk.Button(self.root, text="添加", command=self.add_page) self.Button0.grid(row=11, column=1, ipadx=10) self.Button1 = tk.Button(self.root, text="编辑", command=self.edit_page) self.Button1.grid(row=11, column=3, ipadx=10) self.Button2 = tk.Button(self.root, text="删除", command=self.delete_item) self.Button2.grid(row=11, column=5, ipadx=10) def add_page(self): self.Label0 = tk.Label(self.root, text="姓名") self.Label0.grid(row=0, column=11) self.entry00 = tk.StringVar() self.entry0 = tk.Entry(self.root, textvariable=self.entry00, width=15) self.entry0.grid(row=1, column=11) self.Label1 = tk.Label(self.root, text="年龄") self.Label1.grid(row=2, column=11) self.entry01 = tk.StringVar() self.entry1 = tk.Entry(self.root, textvariable=self.entry01, width=15) self.entry1.grid(row=3, column=11) self.Label02 = tk.Label(self.root, text="城市") self.Label02.grid(row=4, column=11) self.entry02 = tk.StringVar() self.entry2 = tk.Entry(self.root, textvariable=self.entry02, width=15) self.entry2.grid(row=5, column=11) self.Button_1 = tk.Button(self.root, text="确定", command=self.add_item) self.Button_1.grid(row=6, column=11, ipadx=10) def add_item(self): '''添加数据''' rows = self.tree.get_children() # 获取所有的行ID name = self.entry00.get() age = self.entry01.get() city = self.entry02.get() if name and age and city: self.tree.insert("", tk.END, text=len(rows)+1, values=(name, age, city)) else: self.Button_1.bind("<Button-1>", self.window("输入框不可以为空")) def edit_page(self): selected_items = self.tree.selection() if selected_items: for item in selected_items: row_data = self.tree.item(item)['values'] self.Label0 = tk.Label(self.root, text="姓名") self.Label0.grid(row=0, column=11) self.entry00 = tk.StringVar() self.entry00.set(row_data[0]) self.entry0 = tk.Entry(self.root, textvariable=self.entry00, width=15) self.entry0.grid(row=1, column=11) self.Label1 = tk.Label(self.root, text="年龄") self.Label1.grid(row=2, column=11) self.entry01 = tk.StringVar() self.entry01.set(row_data[1]) self.entry1 = tk.Entry(self.root, textvariable=self.entry01, width=15) self.entry1.grid(row=3, column=11) self.Label02 = tk.Label(self.root, text="城市") self.Label02.grid(row=4, column=11) self.entry02 = tk.StringVar() self.entry02.set(row_data[2]) self.entry2 = tk.Entry(self.root, textvariable=self.entry02, width=15) self.entry2.grid(row=5, column=11) self.Button_1 = tk.Button(self.root, text="确定", command=self.edit_item) self.Button_1.grid(row=6, column=11, ipadx=10) else: self.Button1.bind("<Button-1>", self.window("未选中数据")) def edit_item(self): '''编辑数据''' selected_item = self.tree.selection() name = self.entry00.get() age = self.entry01.get() city = self.entry02.get() if name and age and city: self.tree.item(selected_item, values=(name, age, city)) else: self.Button_1.bind("<Button-1>", self.window("输入框不可以为空")) def delete_item(self): '''删除数据''' selected_item = self.tree.selection() if selected_item: self.tree.delete(selected_item) else: self.Button2.bind("<Button-1>", self.window("未选中数据")) def window(e, text): '''创建弹窗''' messagebox.showinfo("提示", text) if __name__ == '__main__': a = GUI() a.root.mainloop()
def interface(self): """"界面编写位置""" # 创建 Treeview 控件;设置高度为10行;只显示列名行,隐藏索引列 self.tree = ttk.Treeview(self.root, height=10, show="headings") self.tree.grid(row=0, column=0, rowspan=10, columnspan=10, padx=60) # 定义列名 self.tree["columns"] = ("Name", "Age", "City") # 设置列的标题名称 self.tree.heading("Name", text="姓名") self.tree.heading("Age", text="年龄") self.tree.heading("City", text="城市") # 设置列宽度(像素) self.tree.column("Name", width=100, anchor="center") self.tree.column("Age", width=90, anchor="center") self.tree.column("City", width=150, anchor="center") # 插入默认数据 self.tree.insert("", tk.END, values=("张三", "25", "深圳"))
import tkinter as tk from tkinter import ttk class GUI: def __init__(self): self.root = tk.Tk() self.root.title('演示窗口') self.root.geometry("600x380+1100+150") self.interface() def interface(self): """界面编写位置""" # 创建 Treeview 控件 self.tree_view = ttk.Treeview(self.root, height=10, show="headings") self.tree_view.grid(row=0, column=0, sticky='nsew') # 添加垂直滚动条控件 self.scroll_bar = ttk.Scrollbar(self.root, orient=tk.VERTICAL, command=self.tree_view.yview) self.scroll_bar.grid(row=0, column=1, sticky='ns') # 滚动条与 Treeview 控件进行关联 self.tree_view.configure(yscrollcommand=self.scroll_bar.set) # 定义列名 self.tree_view["columns"] = ("Name", "Age", "City") # 设置列的标题名称 self.tree_view.heading("Name", text="姓名") self.tree_view.heading("Age", text="年龄") self.tree_view.heading("City", text="城市") # 设置列宽度(像素) self.tree_view.column("Name", width=100, anchor="center") self.tree_view.column("Age", width=90, anchor="center") self.tree_view.column("City", width=150, anchor="center") # 插入默认数据 for i in range(20): self.tree_view.insert("", tk.END, values=("李四", "28", "北京")) self.tree_view.insert("", tk.END, values=("张三", "25", "深圳")) if __name__ == "__main__": run = GUI() run.root.mainloop()
持续更新中…
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。