Python tkinter之Treeview(表格)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python tkinter之Treeview(表格),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7094字,纯文字阅读大概需要11分钟。
内容图文
![Python tkinter之Treeview(表格)](/upload/InfoBanner/zyjiaocheng/618/1a122945225746949b1f186f17cf15d3.jpg)
1、Treeview的基本属性
# -*- encoding=utf-8 -*- import tkinter from tkinter import * from tkinter import ttk if __name__ == '__main__': pass win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 1000 height = 500 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 columns = ['学号', '姓名', '性别', '出生年月', '籍贯', '班级'] table = ttk.Treeview( master=win, # 父容器 height=10, # 高度,可显示height行 columns=columns, # 显示的列 show='headings' ) table.heading('学号', text='学号', ) # 定义表头 table.heading('姓名', text='姓名', ) # 定义表头 table.heading('性别', text='性别', ) # 定义表头 table.heading('出生年月', text='出生年月', ) # 定义表头 table.heading('籍贯', text='籍贯', ) # 定义表头 table.heading('班级', text='班级', ) # 定义表头 table.column('学号', width=100, anchor=S) # 定义列 table.column('姓名', width=150, anchor=S) # 定义列 table.column('性别', width=50, anchor=S) # 定义列 table.column('出生年月', width=150, anchor=S) # 定义列 table.column('籍贯', width=150, anchor=S) # 定义列 table.column('班级', width=150, anchor=S) # 定义列 table.pack(pady=20) win.mainloop()
备注:
①heading 定义显示的表头
②column 定义显示的列,width为列宽度,anchor为对齐方式,可选项为:n, ne, e, se, s, sw, w, nw, center
2、插入数据到表格中
# -*- encoding=utf-8 -*- import tkinter from tkinter import * from tkinter import ttk def show_data(): # 插入数据 info = [ ['1001', '李华', '男', '2014-01-25', '广东', '计算5班', ], ['1002', '小米', '男', '2015-11-08', '深圳', '计算5班', ], ['1003', '刘亮', '男', '2015-09-12', '福建', '计算5班', ], ['1004', '白鸽', '女', '2016-04-01', '湖南', '计算5班', ], ] for index, data in enumerate(info): table.insert('', END, values=data) # 添加数据到末尾 if __name__ == '__main__': pass win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 1000 height = 500 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 columns = ['学号', '姓名', '性别', '出生年月', '籍贯', '班级'] table = ttk.Treeview( master=win, # 父容器 height=10, # 高度,可显示height行 columns=columns, # 显示的列 show='headings' ) table.heading('学号', text='学号', ) # 定义表头 table.heading('姓名', text='姓名', ) # 定义表头 table.heading('性别', text='性别', ) # 定义表头 table.heading('出生年月', text='出生年月', ) # 定义表头 table.heading('籍贯', text='籍贯', ) # 定义表头 table.heading('班级', text='班级', ) # 定义表头 table.column('学号', width=100, anchor=CENTER) # 定义列 table.column('姓名', width=150, anchor=CENTER) # 定义列 table.column('性别', width=50, anchor=CENTER) # 定义列 table.column('出生年月', width=150, anchor=CENTER) # 定义列 table.column('籍贯', width=150, anchor=CENTER) # 定义列 table.column('班级', width=150, anchor=CENTER) # 定义列 table.pack(pady=20) Button(text='显示信息', command=show_data).pack() win.mainloop()
3、删除表格中的数据
# -*- encoding=utf-8 -*- import tkinter from tkinter import * from tkinter import messagebox from tkinter import ttk def show_data(): # 插入数据 info = [ ['1001', '李华', '男', '2014-01-25', '广东', '计算5班', ], ['1002', '小米', '男', '2015-11-08', '深圳', '计算5班', ], ['1003', '刘亮', '男', '2015-09-12', '福建', '计算5班', ], ['1004', '白鸽', '女', '2016-04-01', '湖南', '计算5班', ], ] for index, data in enumerate(info): table.insert('', END, values=data) def delete_all(): # 删除数据 children = table.get_children() # 获取所有的数据 flag = messagebox.askyesno('提示信息', '确认删除所有数据?') if flag: for child in children: table.delete(child) def delete_choose(): selection = table.selection() # 获取选中的数据 print('选中的条数:{}'.format(len(selection))) # 选中的条数 print('选中的对象:{}'.format(selection)) # 选中的数据对象 flag = messagebox.askyesno('提示信息', '确认删除选中数据?') if flag: for item in selection: table.delete(item) if __name__ == '__main__': pass win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 1000 height = 500 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 columns = ['学号', '姓名', '性别', '出生年月', '籍贯', '班级'] table = ttk.Treeview( master=win, # 父容器 height=10, # 高度,可显示height行 columns=columns, # 显示的列 show='headings' ) table.heading('学号', text='学号', ) # 定义表头 table.heading('姓名', text='姓名', ) # 定义表头 table.heading('性别', text='性别', ) # 定义表头 table.heading('出生年月', text='出生年月', ) # 定义表头 table.heading('籍贯', text='籍贯', ) # 定义表头 table.heading('班级', text='班级', ) # 定义表头 table.column('学号', width=100, anchor=CENTER) # 定义列 table.column('姓名', width=150, anchor=CENTER) # 定义列 table.column('性别', width=50, anchor=CENTER) # 定义列 table.column('出生年月', width=150, anchor=CENTER) # 定义列 table.column('籍贯', width=150, anchor=CENTER) # 定义列 table.column('班级', width=150, anchor=CENTER) # 定义列 table.pack(pady=20) Button(text='显示信息', command=show_data).pack() show_data() Button(text='删除选中', command=delete_choose).pack() Button(text='删除所有', command=delete_all).pack() win.mainloop()
4、添加滚动条
# -*- encoding=utf-8 -*- import tkinter from tkinter import * from tkinter import ttk def show_data(): # 插入数据 info = [ ['1001', '李华', '男', '2014-01-25', '广东', '计算5班', ], ['1002', '小米', '男', '2015-11-08', '深圳', '计算5班', ], ['1003', '刘亮', '男', '2015-09-12', '福建', '计算5班', ], ['1004', '白鸽', '女', '2016-04-01', '湖南', '计算5班', ], ] for index, data in enumerate(info): table.insert('', END, values=data) if __name__ == '__main__': pass win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 1200 height = 500 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 frame = Frame(win) frame.pack() scrollbar_y = Scrollbar(frame, orient=VERTICAL) scrollbar_x = Scrollbar(frame, orient=HORIZONTAL) columns = ['学号', '姓名', '性别', '出生年月', '籍贯', '班级'] table = ttk.Treeview( master=frame, # 父容器 height=10, # 高度,可显示height行 columns=columns, # 显示的列 show='headings', yscrollcommand=scrollbar_y.set, # 滚动条 xscrollcommand=scrollbar_x.set, # 滚动条 ) table.heading('学号', text='学号', ) # 定义表头 table.heading('姓名', text='姓名', ) # 定义表头 table.heading('性别', text='性别', ) # 定义表头 table.heading('出生年月', text='出生年月', ) # 定义表头 table.heading('籍贯', text='籍贯', ) # 定义表头 table.heading('班级', text='班级', ) # 定义表头 table.column('学号', width=100, anchor=CENTER) # 定义列 table.column('姓名', width=150, anchor=CENTER) # 定义列 table.column('性别', width=50, anchor=CENTER) # 定义列 table.column('出生年月', width=150, anchor=CENTER) # 定义列 table.column('籍贯', width=150, anchor=CENTER) # 定义列 table.column('班级', width=150, anchor=CENTER) # 定义列 scrollbar_y.config(command=table.yview) scrollbar_x.config(command=table.xview) scrollbar_y.pack(side=RIGHT, fill=Y) scrollbar_x.pack(side=BOTTOM, fill=X) table.pack(fill=BOTH, expand=True) Button(text='显示信息', command=show_data).pack() win.mainloop()
内容总结
以上是互联网集市为您收集整理的Python tkinter之Treeview(表格)全部内容,希望文章能够帮你解决Python tkinter之Treeview(表格)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。