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', '李华', '男', '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()
标签:tkinter,定义,Python,text,表头,width,win,table,Treeview
来源: https://www.cnblogs.com/rainbow-tan/p/14125768.html
python tkinter treeview制作,Python tkinter之Treeview(表格)相关推荐
- python+tkinter+threading制作多线程简易音乐播放器(自动播放,上一曲,下一曲,播放,暂停,实时显示歌曲名并能自动切换歌曲的功能)
https://blog.csdn.net/qq_41962782/article/details/80106158 小弟是小白因大作业,在此博文的基础上,我增加了歌曲目录的选择,自动播放,实时显示歌 ...
- 用python的tkinter库制作仿windows看图器
本文原载于我的简书,简书界面干净,更偏向于简书一些,我的简书 最近在学习python,就用python自己写了一个仿windows的看图器,在网上搜发现找不到相关的代码,所以决定自己尝试做了一个.看图 ...
- python用户登录界面tkinter_python tkinter制作用户登录界面-Go语言中文社区
学习一下莫烦Python的tkinter教程,根据教程制作了用户登录注册页.基本功能为检查登录.注册.清明上河图观看网址http://news.sohu.com/s2015/qmsht/index.s ...
- python 表情包制作工具_python中tkinter模块制作表情包爬取工具遇到的问题
[Python] 纯文本查看 复制代码import tkinter as tk from tkinter.filedialog import askdirectory import requests ...
- 基于 Python 的 tkinter 模块制作的名人名言查询工具
简介:本文主要介绍如何用 Python 内置的 tkinter 写一个查询工具. 很多人学习python,不知道从何学起. 很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手. 很多 ...
- Python tkinter+turtle制作表白神器
值此圣诞元旦即将来临之际,想着编一个表白的代码,虽然自己现在用不上,但说不定能助人为乐呢. 主要是使用python的tkinter库和turtle库,实现效果如下: 初始效果: 点击关闭窗口: 点击不 ...
- python 离线翻译软件_Python使用tkinter制作在线翻译软件
tkinter的功能是如此强大,竟然还能做翻译软件.当然是在线的,我发现有一个quicktranslate模块,可以提供在线翻译功能,相当于提供了一个翻译的接口,利用它就可以制作在线翻译软件了.下面是 ...
- 使用python的tkinter模块制作一个计算器
使用tkinter模块制作的一个简单的计算器 由于刚学这个模块,所以做的不是很好 截图: 点击计算后算式区的值会改成结果,可自行在数字触发的函数中添加数字输入就进行计算回显的功能 代码如下: impo ...
- Python使用鼠标滚轮调整tkinter应用程序窗口大小
图书推荐: <Python程序设计基础与应用>(ISBN:9787111606178),董付国,机械工业出版社 图书详情: 用书教师可以联系董老师获取教学大纲.课件.源码.教案.考试系统等 ...
最新文章
- applet打包的MANIFEST.MF配置
- java web servelt
- 连接sql sever2008数据库出现了无法连接到数据库引擎问题解决
- MSC Apex 2020中文版
- oracle快速插入大量数据
- host 'xx' is not allowed to connect to this MySql server
- PRML-系列一之1.5.5~1.5.6
- unsigned char与char的区别
- FD.io VPP基本介绍-理解VPP软件架构
- PDA应用的一些想法
- RAID0、1、5、6、10介绍
- 无论PC还是Mac,都能畅快地使用移动硬盘
- vector向量容器的一些基本操作
- java中 this详解
- matlab向excel连续写数据,每次换行
- 双向认证---xca--证书产生
- java对象——new对象的理解
- 【c++】string类的模拟实现(下)
- 感动中国感动谁(转)
- 1968年法国五月风暴中的标语口号
热门文章
- ca证书 csr_SSL证书CSR文件生成方式及注意事项
- 学生信息系统管理——优化总结(干货篇二)
- java常用坐标系转换
- 奇门遁甲排盘程序如何确定值符
- 4种典型限流实践保障应用高可用|云效工程师指北
- 在nagios中添加监控主机和服务
- vue-video-player文档_基于vue-video-player自定义播放器的方法
- Deepstream yolov5 两种引擎(engine)生成方式
- Windows下ARM Linux应用程序开发环境搭建说明
- 全国计算机软考试题及答案,全国计算机软考网管语试题及答案.doc