失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python tkinter treeview制作 Python tkinter之Treeview(表格)

python tkinter treeview制作 Python tkinter之Treeview(表格)

时间:2022-01-18 22:36:10

相关推荐

python tkinter treeview制作 Python tkinter之Treeview(表格)

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', '李华', '男', '-01-25', '广东', '计算5班', ],

['1002', '小米', '男', '-11-08', '深圳', '计算5班', ],

['1003', '刘亮', '男', '-09-12', '福建', '计算5班', ],

['1004', '白鸽', '女', '-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', '李华', '男', '-01-25', '广东', '计算5班', ],

['1002', '小米', '男', '-11-08', '深圳', '计算5班', ],

['1003', '刘亮', '男', '-09-12', '福建', '计算5班', ],

['1004', '白鸽', '女', '-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', '李华', '男', '-01-25', '广东', '计算5班', ],

['1002', '小米', '男', '-11-08', '深圳', '计算5班', ],

['1003', '刘亮', '男', '-09-12', '福建', '计算5班', ],

['1004', '白鸽', '女', '-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()

标签:tkinter,定义,Python,text,表头,width,win,table,Treeview

来源: /rainbow-tan/p/14125768.html

如果觉得《python tkinter treeview制作 Python tkinter之Treeview(表格)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。