【Python】tkinter之treeview-目录点击事件

1 参考资料

python ttk Treeview的插入、清空、各种点击事件、获取条目值、标题单击排序

Treeview 官方文档

treeview-树结构

2 基本用法

tv = ttk.Treeview(root, height=20, selectmode = 'browse',show= 'tree')
optiondescription
columns列标识符的列表,用于指定列数及其名称。
displaycolumns列标识符(符号或整数索引)的列表,用于指定显示哪些数据列及其出现的顺序,或字符串“ #all”。
height指定应可见的行数。注意:请求的宽度由列宽度的总和确定。
padding指定窗口小部件的内部填充。填充是最多四个长度规范的列表。
selectmode

控制内置类绑定如何管理选择。 “extended”, “browse” 或 “none”。如果设置为 “extended”( 默认),则可以选择多个项目。如果为“browse”

,则一次将仅选择一个项目。如果为“ none”,则选择不会更改。

show

包含零个或多个以下值的列表,指定要显示树的哪些元素。

  • tree:在第0列中显示树标签。
  • headings: 显示标题行。

默认值为“tree headings”,即显示所有元素。

注意:即使未指定show =“ tree”,列#0始终引用树列。

 

 


3 点击事件

事件代码
鼠标左键单击按下1/Button-1/ButtonPress-1
鼠标左键单击松开ButtonRelease-1
鼠标右键单击3
鼠标左键双击Double-1/Double-Button-1
鼠标右键双击Double-3
鼠标滚轮单击2
鼠标滚轮双击Double-2
鼠标移动B1-Motion
鼠标移动到区域Enter
鼠标离开区域Leave
获得键盘焦点FocusIn
失去键盘焦点FocusOut
键盘事件Key
回车键Return
控件尺寸变话Configure

 

4 案例:点击目录所在行,展示对应功能的结果

 

from tkinter import *
from tkinter import ttk

def ds(root):
	s=Label(root,text='目录1-test')
	s.pack()

def ctm(root):
	c=Label(root,text='目录2-test')
	c.pack()	

def lts(root):
	l=Label(root,text='目录3-test')
	l.pack()

root = Tk()
root.geometry('800x600+250+50')
root.resizable(False,False)
root.title('主界面')

tv = ttk.Treeview(root, height=20, selectmode = 'browse',show= 'tree')
tv.insert('',0,text='----------------------')
tv.insert('',1,text='目录1')
tv.insert('',2,text='目录2')
tv.insert('',3,text='目录3')

tv.place(x=30,y=20)

def all(event):
	item = tv.selection() #'I001'、'I002'
	if item:
		txt = tv.item(item[0],'text')
		print(item,'  ',txt) 
		if txt == '目录1':
			ds(root)
		elif txt == '目录2':
			ctm(root)
		elif txt == '目录3':
			lts(root)
	
tv.bind('<ButtonRelease-1>',all)

root.mainloop()

运行结果:

  • 4
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值