Python GUI 之 Treeview 学习
例子1
from tkinter import *
import tkinter.ttk as ttk
win = Tk()
win.title("Treeview 学习")
col = [1,2,3,4]
data = {"item0":["1a","2a","3a","4a"], "item1":{"num0":["1n", "2n", "3n"," 4n"],"num1":["1m","2m","3m","4m"]}, "item2":["1c","2c","3c","4c"]}
tree = ttk.Treeview(win, columns = col, height = 10, show = "tree")
#show = "tree", 第一列也会被显示出来
#也可用show = "headings" 把第一列隐藏起来
#height 的单位是字符,本例里可以显示10行
tree.column('0',width=150,anchor='center') #指定第一列的宽度和名称, 如果show = "headings", 这一列就被隐藏。
tree.column('1',width=100,anchor='center')
tree.column('2',width=100,anchor='w')
tree.column('3',width=100,anchor='center')
tree.column('4',width=100,anchor='center')
tree.heading('0',text='column0')
tree.heading('1',text='column1')
tree.heading('2',text='column2')
tree.heading('3',text='column3')
tree.heading('4',text='column4')
tree.insert('','end',values= data["item0"])
tree.insert('','end',values= data["item2"])
tree.pack()
win.mainloop()
代码运行结果:
例子2, subtrree
from tkinter import *
import tkinter.ttk as ttk
win = Tk()
win.title("Treeview 学习")
col = [1,2,3,4]
data = {"item0":["1a","2a","3a","4a"], "item1":{"num0":["1n", "2n", "3n"," 4n"],"num1":["1m","2m","3m","4m"]}, "item2":["1c","2c","3c","4c"]}
tree = ttk.Treeview(win, columns = col, height = 10, show = "tree")
#show = "tree", 第一列也会被显示出来
#也可用show = "headings" 把第一列隐藏起来
#height 的单位是字符,本例里可以显示10行
tree.column('0',width=www.hjylp178.com 150,anchor='center') #指定第一列的宽度和名称, 如果show = "headings", 这一列就被隐藏。
tree.column('1',width=www.dfgjyl.cn 100,anchor='center')
tree.column('2',width=www.yongxinzaixian.cn100,anchor='w')
tree.column('3',width=100,anchor='center')
tree.column('4',width=100,anchor='center')
tree.heading('0'www.gangchengyuLe178.com ,text='column0')
tree.heading('1',text=www.ysyl157.com 'column1')
tree.heading('2',text=www.mcyllpt.com 'column2')
tree.heading('3',text=www.meiwanyule.cn 'column3')
tree.heading('4',text='column4')
#用递归法遍历带子字典或列表的数据
def process_dict(d, tree, tr):
for k,v in d.items():
if type(v) == list:
if type(v[0]) == dict:
trr www.yigouyule2.cn = tree.insert(tr, 'end', text=k, open=True)
for ls in v:
process_dict(ls, tree, trr)
else:
tree.insert(tr, 'end', text=k, values= v)
elif type(v) == dict:
trr = tree.insert(tr, 'end', text=k, open = True)
process_dict(v, tree, trr)
process_dict(data,tree, "")
tree.pack()
win.mainloop()
代码运行结果:
例子3, 添加滚动条
from tkinter import *
import tkinter.ttk as ttk
win = Tk()
win.geometry("100x100")
win.title("Treeview 学习")
col = [1,2,3,4]
data = {"item0":["1a","2a","3a","4a"], \
"item1":{"num0":["1n", "2n", "3n"," 4n"],"num1":["1m","2m","3m","4m"]},
"item2":["1c","2c","3c","4c"], \
"item3": ["1a", "2a", "3a", "4a"], \
"item4": {"num40": ["1n", "2n", "3n", " 4n"], "num41": ["1m", "2m", "3m", "4m"]},
"item6": ["1c", "2c", "3c", "4c"],\
"item7":["1a","2a","3a","4a"], \
"item8":{"num80":["1n", "2n", "3n"," 4n"],"num81":["1m","2m","3m","4m"]},
"item9":["1c","2c","3c","4c"],\
"item10":["1a","2a","3a","4a"], \
"item11":{"num110":["1n", "2n", "3n"," 4n"],"num111":["1m","2m","3m","4m"]},
"item12":["1c","2c","3c","4c"]
}
tree = ttk.Treeview(win, columns = col, height = 10, show = "tree")
#show = "tree", 第一列也会被显示出来
#也可用show = "headings" 把第一列隐藏起来
#height 的单位是字符,本例里可以显示10行
tree.column('0',width=150,anchor='center') #指定第一列的宽度和名称, 如果show = "headings", 这一列就被隐藏。
tree.column('1',width=100,anchor='center')
tree.column('2',width=100,anchor='w')
tree.column('3',width=100,anchor='center')
tree.column('4',width=100,anchor='center')
tree.heading('0',text='column0')
tree.heading('1',text='column1')
tree.heading('2',text='column2')
tree.heading('3',text='column3')
tree.heading('4',text='column4')
#用递归法遍历带子字典或列表的数据
def process_dict(d, tree, tr):
for k,v in d.items():
if type(v) == list:
if type(v[0]) == dict:
trr = tree.insert(tr, 'end', text=k, open=True)
for ls in v:
process_dict(ls, tree, trr)
else:
tree.insert(tr, 'end', text=k, values= v)
elif type(v) == dict:
trr = tree.insert(tr, 'end', text=k, open = True)
process_dict(v, tree, trr)
process_dict(data,tree, "")
#y滚动条
yscrollbar = Scrollbar(win, orient=VERTICAL, command=tree.yview)
tree.configure(yscrollcommand = yscrollbar.set)
yscrollbar.pack(side = RIGHT, fill = Y)
#x滚动条
xscroll = Scrollbar(win, orient=HORIZONTAL, command=tree.xview)
tree.configure(xscrollcommand = xscroll.set)
xscroll.pack(side = BOTTOM, fill = X)
tree.pack(side = TOP, expand = 1, fill = BOTH)
win.mainloop()
代码运行结果:
---------------------
作者:weixin_41501380
来源:CSDN
原文:https://blog.csdn.net/weixin_41501380/article/details/83933484
版权声明:本文为博主原创文章,转载请附上博文链接!
Python GUI 之 Treeview 学习的更多相关文章
- Python:GUI之tkinter学习笔记1控件的介绍及使用
相关内容: tkinter的使用 1.模块的导入 2.使用 3.控件介绍 Tk Button Label Frame Toplevel Menu Menubutton Canvas Entry Mes ...
- Python:GUI之tkinter学习笔记之messagebox、filedialog
相关内容: messagebox 介绍 使用 filedialog 介绍 使用 首发时间:2018-03-04 22:18 messagebox: 介绍:messagebox是tkinter中的消息框 ...
- Python:GUI之tkinter学习笔记3事件绑定
相关内容: command bind protocol 首发时间:2018-03-04 19:26 command: command是控件中的一个参数,如果使得command=函数,那么点击控件的时候 ...
- Python:GUI之tkinter学习笔记2界面布局显示
相关内容: pack 介绍 常用参数 使用情况 常用函数 grid 介绍 常用参数 使用情况 常用函数 place 介绍 常用参数 使用情况 常用函数 首发时间:2018-03-04 14:20 pa ...
- python3.4学习笔记(九) Python GUI桌面应用开发工具选择
python3.4学习笔记(九) Python GUI桌面应用开发工具选择 Python GUI开发工具选择 - WEB开发者http://www.admin10000.com/document/96 ...
- 使用PyQt来编写第一个Python GUI程序
原文:使用PyQt来编写第一个Python GUI程序 本文由 伯乐在线 - Lane 翻译,Daetalus 校稿.未经许可,禁止转载!英文出处:pythonforengineers.com.欢迎加 ...
- Python GUI开发环境的搭建
原文:Python GUI开发环境的搭建 最近对Python的开发又来了兴趣,对于Python的开发一直停留在一个表面层的认识,玩的部分比较大. Python的入手简单,语法让人爱不释手,在网络通信方 ...
- python GUI实战项目——tkinter库的简单实例
一.项目说明: 本次通过实现一个小的功能模块对Python GUI进行实践学习.项目来源于软件制造工程的作业.记录在这里以复习下思路和总结编码过程.所有的源代码和文件放在这里: 链接: https:/ ...
- python GUI图形化编程-----wxpython
一.python gui(图形化)模块介绍: Tkinter :是python最简单的图形化模块,总共只有14种组建 Pyqt :是python最复杂也是使用最广泛的图形化 Wx ...
随机推荐
- MySQL主服务配置文件
[mysql]port=3306socket=/var/lib/mysql/mysql.sockdefault-character-set = utf8mb4 [mysqld]server-id = ...
- ARC和MRC混合模式下的编译问题
在一个支持ARC (Automatic Reference Counting)的项目中,有时候需要禁止其中几个文件使用ARC模式编译(比如你用了第三方不支持ARC的类库).这时就要点击工程文件,在ta ...
- python简单爬虫爬取百度百科python词条网页
目标分析:目标:百度百科python词条相关词条网页 - 标题和简介 入口页:https://baike.baidu.com/item/Python/407313 URL格式: - 词条页面URL:/ ...
- Linux平台搭建roboframework
安装步骤介绍: . 在Centos7..1503下,默认的python的版本2./site-packages/). 2.安装pip 第一步: ()下载setuptools包 # wget http:/ ...
- nyoj-1103-区域赛系列一多边形划分
http://acm.nyist.net/JudgeOnline/problem.php?pid=1103 区域赛系列一多边形划分 时间限制:1000 ms | 内存限制:65535 KB 难度: ...
- 响应式Web设计- 图片
使用width属性:如果width属性设置为100%,图片会根据上下范围实现响应式的功能. <!DOCTYPE html><html><head><meta ...
- 118. Pascal's Triangle@python
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. Example: Inpu ...
- 通过工厂模式批量创建对象后调用其中方法 出现XXXis not a function()问题原因
//通过工厂模式批量创建 function Computer(color,weight,logo){ var obj=new Object(); obj.color=c ...
- NOIP 成绩
这道题中点是在小数上,因为成绩可能是:“95.5 87.7……”所以我们就要用:printf和scanf这样就可以控制小数了!!! code: #include<bits/stdc++.h> ...
- Luogu P4231 三步必杀 (差分)
目录 题目 题解 题目 题目链接 题目背景 (三)旧都 离开狭窄的洞穴,眼前豁然开朗. 天空飘着不寻常的雪花. 一反之前的幽闭,现在面对的,是繁华的街市,可以听见酒碗碰撞的声音. 这是由被人们厌恶的鬼 ...