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
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/qwangxiao/p/9940972.html

时间: 2024-08-30 12:27:56

Python GUI 之 Treeview 学习的相关文章

Python:GUI之tkinter学习笔记1控件的介绍及使用

相关内容: tkinter的使用 1.模块的导入 2.使用 3.控件介绍 Tk Button Label Frame Toplevel Menu Menubutton Canvas Entry Message Text Listbox Checkbutton Radiobutton Scale Scrollbar 首发时间:2018-03-04 16:39 Python的GUI各有各特点. 由于只是轻微涉及GUI开发,所以就以轻量级的tkinter来学习. tkinter的使用: 1.模块的导入

Python:GUI之tkinter学习笔记2界面布局显示

相关内容: pack 介绍 常用参数 使用情况 常用函数 grid 介绍 常用参数 使用情况 常用函数 place 介绍 常用参数 使用情况 常用函数 首发时间:2018-03-04 14:20 pack: 介绍: pack几何管理器按行或列打包小部件. 可以使用填充fill,展开expand和靠边side等选项来控制此几何体管理器. pack的排放控件的形式就像将一个个控件按大小从上到下放过去 在窗口不设定大小的而使用pack进行布局的情况下,窗口默认大小为刚好包裹所有控件的大小 默认情况下添

python基础教程_学习笔记23:图形用户界面

图形用户界面 丰富的平台 在编写Python GUI程序前,需要决定使用哪个GUI平台. 简单来说,平台是图形组件的一个特定集合,可以通过叫做GUI工具包的给定Python模块进行访问. 工具包 描述 Tkinter 使用Tk平台.很容易得到.半标准. wxpython 基于wxWindows.跨平台越来越流行. PythonWin 只能在Windows上使用.使用了本机的Windows GUI功能. JavaSwing 只能用于Jython.使用本机的Java GUI. PyGTK 使用GTK

python GUI尝鲜(但当涉猎,见往事耳)

第一步:简单的窗口和内容 import tkinter as tk window = tk.Tk() # 窗口obj对象 window.title('my TK') # 窗口名字 window.geometry('200x100') # 窗口宽度和高度 # Label对象传入相应参数text:文本内容; bg:背景; font:字体; width.height 内容的宽度.高度 l = tk.Label(window,text='OMG this is TK!',bg='green',font=

分享13个Python GUI库

Python Python是一门高级编程语言.它用于通用编程.Python语言由Guido van Rossum创建,并于1991年首次发布.Python的设计哲学着重于代码的可读性.因此空白在Python中具有重要的意义. Python提供了允许在小规模和大规模上编程的设计理念,而且具有一个非常庞大的标准库.Python使用动态类型系统,并具有自动内存管理功能. Python支持多种编程范式,其中包括: 面向对象命令式函数式程序式图形用户界面(GUI) GUI是一个人机交互的界面,换句话说,它

python之raw_input()(学习笔记六)

python之raw_input()(学习笔记六) 我们经常使用raw_input()读取用户的输入,如下例子所示: >>> name = raw_input('please input your name:'),截图如下: 下面简单说下,raw_input()与if搭配使用,脚本如下: #!/usr/bin/env python # -*- coding:utf-8 -*- birth = raw_input('birth:') if birth < 2000: print '0

python基础教程_学习笔记3:元组

元组 元组不能修改:(可能你已经注意到了:字符串也不能修改.) 创建元组的语法很简单:如果用逗号分隔了一些值,那么你就自动创建了元组. >>> 1,3,'ab' (1, 3, 'ab') 元组也是(大部分时候是)通过圆括号括起来的. >>> (1,3,'13') (1, 3, '13') 空元组可以用没有内容的两个圆括号来表示. 如何实现包括一个值的元组呢? >>> (5) 5 >>> ('ab') 'ab' >>>

Python GUI with Tkinter (from youtube) 在youtube上能找到很多编程视频...

Python GUI with Tkinter - 1 - Introduction以上链接是一个python tkinter视频系列的第一讲的链接.虽然英语不好,但是,程序还是看得懂的(照着做就可以了),所以找不到中文视频时看下这些英语视频也是可以的. 以下是我在看视频过程中的练习, 可以在python2.7下运行. 001: hello,world: from Tkinter import Label, Tk root = Tk() thelabel = Label(root, text="

python基础教程_学习笔记9:抽象

抽象 懒惰即美德. 抽象和结构 抽象可以节省大量工作,实际上它的作用还要更大,它是使得计算机程序可以让人读懂的关键. 创建函数 函数可以调用(可能包含参数,也就是放在圆括号中的值),它执行某种行为并且返回一个值.一般来说,内建的callable函数可以用来判断函数是否可调用: >>> import math >>> y=1 >>> x=math.sqrt >>> callable(x) True >>> callab