欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python tkinter 樹(shù)形列表控件(Treeview)的使用方法

 更新時(shí)間:2021年04月09日 10:06:39   作者:風(fēng)華明遠(yuǎn)  
這篇文章主要介紹了Python tkinter 樹(shù)形列表控件(Treeview)的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

1.方法

方法 描述
bbox(item, column=None) 返回指定item的框選范圍,或者單元格的框選范圍
column( cid, option=None, **kw) 設(shè)置或者查詢某一列的屬性
delete(*items) 刪除指定行或者節(jié)點(diǎn)(含子節(jié)點(diǎn))
vdetach(*items) 與delete類似,不過(guò)不是真正刪除,而是隱藏了相關(guān)內(nèi)容??梢杂胢ove方法重新顯示v
exists(item) 判斷指定的item是否存在
focus(item=None) 獲得選定item的iid,或者選中指定item。
get_children(item=None) 返回指定item的子節(jié)點(diǎn)
heading(column, option=None, **kw) 設(shè)置或者查詢表頭行的配置參數(shù)
identify(component, x, y) 返回在坐標(biāo)(x,y)處的部件信息。部件包括:region(heading,cell等), item, column, row, 和 element。
identify_element(x, y) 返回在(x,y)處的元素。
identify_region(x, y) 返回坐標(biāo)(x,y)處的Tree view組成部分
identify_row(y) 給定y坐標(biāo),返回指定item索引
index(item) 返回?cái)?shù)字化的item索引,從0開(kāi)始
set_children(item, *newchildren) 設(shè)置item的新子節(jié)點(diǎn)為newchildren,現(xiàn)有的子節(jié)點(diǎn)會(huì)被移除。一般用于樹(shù)形結(jié)構(gòu)中。
insert(parent, index, iid=None, **kw) 插入一個(gè)新的item
item(item, option=None, **kw) 返回item節(jié)點(diǎn)的相關(guān)信息
move(item, parent, index) move()方法有兩種作用:
(1)將detach的item重新顯示(reattach)
(2)移動(dòng)item指定的節(jié)點(diǎn)到parent的子節(jié)點(diǎn)中,位置由index指定
next(item) 返回item的下一個(gè)節(jié)點(diǎn)
parent(item) 返回item的父節(jié)點(diǎn)
prev(item) 返回item的前一個(gè)節(jié)點(diǎn)
see(item) 保證item指定的節(jié)點(diǎn)在Treeview控件中可見(jiàn)
selection(items=None) 返回當(dāng)前選定的節(jié)點(diǎn)的iid
selection_set(*items) 選中items指定的節(jié)點(diǎn)
selection_remove(*items) 從當(dāng)前選擇中移除items指定的節(jié)點(diǎn)
selection_add(*items) 將items指定的節(jié)點(diǎn)添加到當(dāng)前的選擇中
selection_toggle(*items) 選中的節(jié)點(diǎn)變?yōu)椴贿x中,未選中的節(jié)點(diǎn)變?yōu)檫x中
set(item, column=None, value=None) 設(shè)置或者獲得節(jié)點(diǎn)信息
tag_bind( tagname, sequence=None, callback=None) 給tagname指定的tag綁定事件以及回調(diào)函數(shù)
tag_configure( tagname, option=None, **kw) 配置或者獲得tag的有關(guān)信息
tag_has(tagname, item=None) 判斷tag是否存在或者是tag與那些節(jié)點(diǎn)關(guān)聯(lián)

1.1 bbox(item, column=None)

獲取指定item的框選范圍。如果指定的item可見(jiàn),返回值是一個(gè)四元組(x,y,w,h)。(x,y)是矩形框選的左上角坐標(biāo),(w,h)是矩形的寬度與高度。有這個(gè)四元組設(shè)定的矩形正好可以框選指定的item。如果column不為空,返回的是指定單元格的框選范圍。

坐標(biāo)值是以Treeview控件為基準(zhǔn)的,而不是以窗口或者屏幕。

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('320x240')
tk.Label(root,text='成績(jī)表').pack()
area=('#','數(shù)學(xué)','語(yǔ)文','英語(yǔ)')
ac=('all','m','c','e')
data=[('張三','90','88','95'),
      ('李四','100','92', '90'),
      ('王二','88','90', '91')
      ]
tv=ttk.Treeview(root,columns=ac,show='headings',
                height=7,padding=(10,5,20,30))
for i in range(4):
    tv.column(ac[i],width=70,anchor='e')
    tv.heading(ac[i],text=area[i])
tv.pack()
for i in range(3):
    tv.insert('','end',values=data[i])
def select(*args):
    print(tv.bbox(tv.selection()))
    print(tv.bbox(tv.selection(),column='c'))   
tv.bind('<<TreeviewSelect>>',select)
root.mainloop()

 結(jié)果:
(11, 50, 280, 20)
(151, 50, 70, 20)

說(shuō)明:輸出的結(jié)果有2個(gè),一個(gè)是選中行的框選范圍,一個(gè)是選中行的第三個(gè)單元格的框選范圍。

1.2 column( cid, option=None, **kw)

查詢或者修改指定列的配置。cid可以是整數(shù),也可以列的別名。如果不輸入option,則返回目前的配置選項(xiàng)字典。
Treeview列的選項(xiàng)有:

選項(xiàng) 含義
anchor 對(duì)齊模式。取值有n,e,s,w,ne,nw,se,sw和center。
id 列的名稱或者標(biāo)識(shí)
minwidth 列的最小寬度,調(diào)整列寬的時(shí)候,不會(huì)小于這個(gè)數(shù)值。默認(rèn)值是20
stretch 是否隨著窗口大小的調(diào)整而拉伸Treeview。默認(rèn)值是True
width 定義列寬。默認(rèn)值是200

查詢代碼:

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('320x240')
tk.Label(root,text='成績(jī)表').pack()
area=('#','數(shù)學(xué)','語(yǔ)文','英語(yǔ)')
ac=('all','m','c','e')
data=[('張三','90','88','95'),
      ('李四','100','92', '90'),
      ('王二','88','90', '91')
      ]
tv=ttk.Treeview(root,columns=ac,show='headings',
                height=7,padding=(10,5,20,30))
for i in range(4):
    tv.column(ac[i],width=70,anchor='e')
    tv.heading(ac[i],text=area[i])
tv.pack()
for i in range(3):
    tv.insert('','end',values=data[i])   
print(tv.column(3))
root.mainloop()

結(jié)果:
{'width': 70, 'minwidth': 20, 'stretch': 1, 'anchor': 'e', 'id': 'e'}

設(shè)置代碼:

import tkinter as tk
from tkinter import ttk 
root = tk.Tk()
root.geometry('320x240')
tk.Label(root,text='成績(jī)表').pack()
area=('#','數(shù)學(xué)','語(yǔ)文','英語(yǔ)')
ac=('all','m','c','e')
data=[('張三','90','88','95'),
      ('李四','100','92', '90'),
      ('王二','88','90', '91')
      ]
tv=ttk.Treeview(root,columns=ac,show='headings',
                height=5)
for i in range(4):
    tv.column(ac[i],width=70,anchor='e')
    tv.heading(ac[i],text=area[i])
tv.pack()
for i in range(3):
    tv.insert('','end',values=data[i])
def column():
    tv.column(2,width=50)
ttk.Button(root,text='Column',command=column).pack()
root.mainloop()

結(jié)果:

在這里插入圖片描述在這里插入圖片描述

說(shuō)明:點(diǎn)擊'Column'按鈕后,語(yǔ)文成績(jī)那一列的寬度由70變?yōu)?0。

1.3 delete(items)

刪除指定的item。子節(jié)點(diǎn)也會(huì)被一起刪除,但是第一層節(jié)點(diǎn)不會(huì)被刪除。

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('320x240')
tk.Label(root,text='成績(jī)表').pack()
area=('#','數(shù)學(xué)','語(yǔ)文','英語(yǔ)')
ac=('all','m','c','e')
data=[('張三','90','88','95'),
      ('李四','100','92', '90'),
      ('王二','88','90', '91')
      ]
tv=ttk.Treeview(root,columns=ac,show='headings',
                height=5)
for i in range(4):
    tv.column(ac[i],width=70,anchor='e')
    tv.heading(ac[i],text=area[i])
tv.pack()
for i in range(3):
    tv.insert('','end',values=data[i])
def delete():
    tv.delete(tv.selection())
ttk.Button(root,text='Delete',command=delete).pack()
root.mainloop()

結(jié)果:

在這里插入圖片描述在這里插入圖片描述

1.4 detach(items)

detach的方法與delete類似,不過(guò)detach不是真正的刪除了指定的item,而是隱藏了內(nèi)容,可以使用move的方法重新將隱藏的數(shù)據(jù)再顯示出來(lái)。

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('320x240')
tk.Label(root,text='成績(jī)表').pack()
area=('#','數(shù)學(xué)','語(yǔ)文','英語(yǔ)')
ac=('all','m','c','e')
data=[('張三','90','88','95'),
      ('李四','100','92', '90'),
      ('王二','88','90', '91')
      ]
tv=ttk.Treeview(root,columns=ac,show='headings',
                height=5)
for i in range(4):
    tv.column(ac[i],width=70,anchor='e')
    tv.heading(ac[i],text=area[i])
tv.pack()
for i in range(3):
    tv.insert('','end',values=data[i])
detach=None
index=None
def detach():
    global detach
    global index
    detach=tv.selection()
    index=tv.index(detach)
    tv.detach(detach)
def attach():
    global detach
    global index
    if detach:
        tv.move(detach,'',index)
ttk.Button(root,text='Detach',command=detach).pack()
ttk.Button(root,text='Attach',command=attach).pack()
root.mainloop()

說(shuō)明:先選中一行,然后點(diǎn)擊'Detach'按鈕,此時(shí)會(huì)將選中的行隱藏。但是相關(guān)的id和index會(huì)依舊被記錄。再點(diǎn)擊'Attach'按鈕,使用move()方法就會(huì)重新顯示隱藏的數(shù)據(jù)。

1.5 exists(item)

判斷指定的item是否存在。需要注意的是,使用detach()方法隱藏的item會(huì)被認(rèn)為是存在的,因?yàn)橄鄳?yīng)的id等信息是依然被系統(tǒng)記錄沒(méi)有清空的。返回值為T(mén)rue,如果指定的item不存在,否則返回False。

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('320x240')
tk.Label(root,text='成績(jī)表').pack()
area=('#','數(shù)學(xué)','語(yǔ)文','英語(yǔ)')
ac=('all','m','c','e')
data=[('張三','90','88','95'),
      ('李四','100','92', '90'),
      ('王二','88','90', '91')
      ]
tv=ttk.Treeview(root,columns=ac,show='headings',
                height=5)
for i in range(4):
    tv.column(ac[i],width=70,anchor='e')
    tv.heading(ac[i],text=area[i])
tv.pack()
for i in range(3):
    tv.insert('','end',values=data[i])
print(tv.exists('I002'))
root.mainloop()

結(jié)果:
結(jié)果為T(mén)rue

1.6 focus(item=None)

focus()方法有三種情況:
(1)有item被選中同時(shí)參數(shù)為None
返回當(dāng)前被選中的item的標(biāo)識(shí)iid
(2)無(wú)item被選中同時(shí)參數(shù)為None
返回空字符串''
(3)輸入item參數(shù)
指定的item會(huì)獲得focus。
注意:獲得focus不表示被選中。

#有item被選中同時(shí)參數(shù)為None,返回iid
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('320x240')
tk.Label(root,text='成績(jī)表').pack()
area=('#','數(shù)學(xué)','語(yǔ)文','英語(yǔ)')
ac=('all','m','c','e')
data=[('張三','90','88','95'),
      ('李四','100','92', '90'),
      ('王二','88','90', '91')
      ]
tv=ttk.Treeview(root,columns=ac,show='headings',
                height=5)
for i in range(4):
    tv.column(ac[i],width=70,anchor='e')
    tv.heading(ac[i],text=area[i])
tv.pack()
for i in range(3):
    tv.insert('','end',values=data[i])
def focus():
    print(tv.focus())
ttk.Button(root,text='Focus',command=focus).pack()

root.mainloop()

結(jié)果:

在這里插入圖片描述

結(jié)果為:I003

在這里插入圖片描述

#無(wú)item被選中同時(shí)參數(shù)為None
#item參數(shù)不為空
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('320x240')
tk.Label(root,text='成績(jī)表').pack()
area=('#','數(shù)學(xué)','語(yǔ)文','英語(yǔ)')
ac=('all','m','c','e')
data=[('張三','90','88','95'),
      ('李四','100','92', '90'),
      ('王二','88','90', '91')
      ]
tv=ttk.Treeview(root,columns=ac,show='headings',
                height=5)
for i in range(4):
    tv.column(ac[i],width=70,anchor='e')
    tv.heading(ac[i],text=area[i])
tv.pack()
for i in range(3):
    tv.insert('','end',values=data[i])
def focus():
    tv.focus('I002')
    print(tv.focus())
ttk.Button(root,text='Focus',command=focus).pack()
root.mainloop()

注:使用focus()方法,并不會(huì)讓獲得focus的item被高亮顯示。如果要高亮顯示,請(qǐng)使用selection_set()方法。

1.7 get_children(item=None)

返回指定item的子節(jié)點(diǎn)。如果該item沒(méi)有子節(jié)點(diǎn)返回None。如果沒(méi)有指定節(jié)點(diǎn),默認(rèn)返回root節(jié)點(diǎn)的子節(jié)點(diǎn)。

1.8 heading(column, option=None, **kw)

設(shè)置或者查詢表頭行的配置參數(shù)。如果是表格形式的,column是列的位置(就是第幾列,從0計(jì)數(shù))或者列的別名。如果是樹(shù)形結(jié)構(gòu)的(比如文件目錄),column的值為'#0'。
如果沒(méi)有option參數(shù),返回的是當(dāng)前的配置數(shù)據(jù)。
heading的選項(xiàng)有:

選項(xiàng) 含義
anchor 對(duì)齊模式。取值有n,e,s,w,ne,nw,se,sw和center。
command 與指定列相關(guān)的回調(diào)函數(shù)
image 在表頭顯示圖片
text 在表頭顯示文本
state 當(dāng)前列的狀態(tài)

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('320x240')
tk.Label(root,text='成績(jī)表').pack()
area=('#','數(shù)學(xué)','語(yǔ)文','英語(yǔ)')
ac=('all','m','c','e')
data=[('張三','90','88','95'),
      ('李四','100','92', '90'),
      ('王二','88','90', '91')
      ]
tv=ttk.Treeview(root,columns=ac,show='headings',
                height=5)
for i in range(4):
    tv.column(ac[i],width=70,anchor='e')
    tv.heading(ac[i],text=area[i])
tv.pack()
for i in range(3):
    tv.insert('','end',values=data[i])
def heading():
    print(tv.heading(column=1))
ttk.Button(root,text='Heading',command=heading).pack()
root.mainloop()

結(jié)果:
{'text': '數(shù)學(xué)', 'image': '', 'anchor': 'center', 'command': '', 'state': ''}

1.9 identify(component, x, y)

返回在坐標(biāo)(x,y)處的部件信息。部件包括:region(heading,cell等), item, column, row, 和 element。

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('320x240')
tk.Label(root,text='成績(jī)表').pack()
area=('#','數(shù)學(xué)','語(yǔ)文','英語(yǔ)')
ac=('all','m','c','e')
data=[('張三','90','88','95'),
      ('李四','100','92', '90'),
      ('王二','88','90', '91')
      ]
tv=ttk.Treeview(root,columns=ac,show='headings',
                height=5)
for i in range(4):
    tv.column(ac[i],width=70,anchor='e')
    tv.heading(ac[i],text=area[i])
tv.pack()
for i in range(3):
    tv.insert('','end',values=data[i])
def identify():
    print(tv.identify('region',120,30))
ttk.Button(root,text='Identify',command=identify).pack()
root.mainloop()

結(jié)果:
cell

1.10 identify_column(x)

給定x坐標(biāo),返回所屬的列號(hào)。返回值的格式是'#n'。n是從1開(kāi)始計(jì)數(shù)的列號(hào)。注意返回的是實(shí)際的顯示列號(hào),而不是邏輯定義的列號(hào)。如果使用了displaycolumns就可以實(shí)際顯示的列號(hào)與columns定義的列號(hào)是不一致的情況。具體的用法與identify類似,可以參考。

1.11 identify_element(x, y)

返回在(x,y)處的元素。使用方法是: tv.identify_element(140,25)

1.12 identify_region(x, y)

返回坐標(biāo)(x,y)處的Tree view組成部分。Treeview 的組成部分有:

組成部分 含義
nothing 不在Treeview控件內(nèi)
heading 位于表頭的位置
separator 在分隔線上
tree 位于圖標(biāo)列(樹(shù)形列表表示展開(kāi)/折疊的圖標(biāo))
cell 位于單元格內(nèi)

使用方法是:

tv.identify_region(140,25)

1.13 identify_row(y)

給定y坐標(biāo),返回指定item索引(如‘I002')。如果沒(méi)有內(nèi)容,返回空字符串。
使用方法:

tv.index(‘I002')

1.14 index(item)

返回?cái)?shù)字化的item索引,計(jì)數(shù)從0開(kāi)始。使用方法:

tv.index(‘I002')

1.15 set_children(item, newchildren)

設(shè)置item的新子節(jié)點(diǎn)為newchildren,現(xiàn)有的子節(jié)點(diǎn)會(huì)被移除。一般用于樹(shù)形結(jié)構(gòu)中。

tv.set_children('I003','I00E')

說(shuō)明:將I00E作為新的I003的子節(jié)點(diǎn)。

1.16 insert(parent, index, iid=None, **kw)

插入一個(gè)新的item。
(1)parent
對(duì)于表格類型的Treeview,parent一般為空。對(duì)于樹(shù)形類型的Treeview,parent為父節(jié)點(diǎn)。
(2)index
指明在何處插入新的item??梢允?end',也可以是數(shù)字。比如,如果要讓新插入的item成為第一個(gè)子節(jié)點(diǎn)或者在第一行,index就設(shè)為0。如果是第二個(gè)子節(jié)點(diǎn)或者第二行,就是設(shè)置index=1。如果在最末端插入item,就設(shè)置index='end'
(3)iid
如果沒(méi)有賦值,就使用系統(tǒng)自動(dòng)生成的id。如果輸入了id,必須保證與現(xiàn)有的id不重復(fù)。否則系統(tǒng)會(huì)自動(dòng)生成id。
(4)**kw
設(shè)置插入item的屬性。支持的屬性有:

選項(xiàng) 含義
image 顯示圖像
open 針對(duì)樹(shù)形結(jié)構(gòu),確認(rèn)插入的item是打開(kāi)還是折疊狀態(tài)。True打開(kāi),F(xiàn)alse為折疊。
tags 為新插入的item設(shè)置tag標(biāo)簽
text 顯示文字
values 在表格結(jié)構(gòu)中,要顯示的數(shù)值。這些數(shù)值是按照邏輯結(jié)構(gòu)賦值的,也就是按照columns設(shè)定的列次序來(lái)賦值。如果輸入的個(gè)數(shù)少于columns指定列數(shù),則插入空白字符串

在前面章節(jié)的例子中已經(jīng)使用了insert,可以參考。

1.17 item(item, option=None, **kw)

item()方法有3種功能:
(1)只有item參數(shù)
返回item有關(guān)數(shù)據(jù)字典。數(shù)據(jù)字典的鍵值(key)包括:
text,image,open,tags以及values。values就是item參數(shù)指定的節(jié)點(diǎn)的內(nèi)容。

tv.item('I002')

結(jié)果:
{'text': '', 'image': '', 'values': ['李四', 100, 92, 90], 'open': 0, 'tags': ''}

(2)輸入item和option參數(shù)
返回item指定節(jié)點(diǎn)中由option指定的選項(xiàng)值。比如:

tv.item('I002',option='values')

就是返回節(jié)點(diǎn)的內(nèi)容。見(jiàn)前一節(jié)有關(guān)返回?cái)?shù)據(jù)字典的鍵值。

(3)item和kw
使用
kw中的鍵值對(duì)(就是選項(xiàng)值)修改item指定的節(jié)點(diǎn)的相關(guān)選項(xiàng)。最常用的是使用values來(lái)修改節(jié)點(diǎn)的內(nèi)容。
比如:

tv.item('I002',values=('李峰','90','88','66'))

結(jié)果:

在這里插入圖片描述在這里插入圖片描述

可以看到第二行的數(shù)據(jù)被修改了。

1.18 move(item, parent, index)

move()方法有兩種作用:
(1)將detach的item重新顯示(reattach)
(2)移動(dòng)item指定的節(jié)點(diǎn)到parent的子節(jié)點(diǎn)中,位置由index指定
需要注意的是,不能把父節(jié)點(diǎn)移動(dòng)到子節(jié)點(diǎn)下面。因?yàn)檫@是無(wú)法實(shí)現(xiàn)的。還有就是index的值如果是0或者負(fù)數(shù),則表示item的位置在parent的第一個(gè)子節(jié)點(diǎn)。如果index的值大于或者等于子節(jié)點(diǎn)的總數(shù),則表示把item放置在子節(jié)點(diǎn)的最后一個(gè)。

1.19 next(item)

(1)如果item不是當(dāng)前父節(jié)點(diǎn)的最后一個(gè)子節(jié)點(diǎn),則返回下一個(gè)子節(jié)點(diǎn)
(2)如果item已經(jīng)是最后一個(gè)子節(jié)點(diǎn),返回空字符串
(3)如果是表格類型的Treeview,則返回下一item對(duì)象?;蛘叻祷乜兆址绻鹖tem已經(jīng)是最后一個(gè)item對(duì)象。

1.20 parent(item)

樹(shù)形結(jié)構(gòu)的Treeview,該方法返回父節(jié)點(diǎn)的ID;如果是表格類型的Treeview,則返回空字符串。

1.21 prev(item)

與next()類似,不過(guò)是返回前一個(gè)item的ID。

1.22 see(item)

保證item指定的節(jié)點(diǎn)在Treeview控件中可見(jiàn)。針對(duì)有比較多節(jié)點(diǎn)的情況下,此方法可以讓希望顯示的節(jié)點(diǎn)快速顯示在窗口中而不需要用滾動(dòng)條的去滾動(dòng)。

1.23 selection(items=None)

返回當(dāng)前選定的節(jié)點(diǎn)的iid。

1.24 selection_set(items)

選中item指定的節(jié)點(diǎn)。items可以是單個(gè)節(jié)點(diǎn)的ID,或者多個(gè)節(jié)點(diǎn)ID的元組。

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('320x240')
tk.Label(root,text='成績(jī)表').pack()
area=('#','數(shù)學(xué)','語(yǔ)文','英語(yǔ)')
ac=('all','m','c','e')
data=[('張三','90','88','95'),
      ('李四','100','92', '90'),
      ('王二','88','90', '91')
      ]
tv=ttk.Treeview(root,columns=ac,show='headings',
                height=5)
for i in range(4):
    tv.column(ac[i],width=70,anchor='e')
    tv.heading(ac[i],text=area[i])
tv.pack()
for i in range(3):
    tv.insert('','end',values=data[i])
def selection():
    tv.selection_set('I001','I002')
ttk.Button(root,text='Selection',command=selection).pack()
root.mainloop()

結(jié)果:

在這里插入圖片描述

1.25 selection_remove(items)

與selection_set()相反,selection_remove()是把items指定的節(jié)點(diǎn)從選擇中移除。

1.26 selection_add(items)

將items指定所有節(jié)點(diǎn)加入到選中的集合中。那么selection_add()與selection_set()的區(qū)別是什么?selection_set相當(dāng)于重置選擇的節(jié)點(diǎn),不管以前是如何選擇的,執(zhí)行selection_set后,只有selection_set中輸入的items節(jié)點(diǎn)會(huì)被選中。而selection_add()對(duì)當(dāng)前的選選擇沒(méi)有影響,只是把items指定的節(jié)點(diǎn)添加到選擇的集合中。

1.27 selection_toggle(items)

該方法的作用相當(dāng)于數(shù)字電路中的非門(mén),就是已經(jīng)選中的節(jié)點(diǎn)變?yōu)闉檫x中,沒(méi)有選中的節(jié)點(diǎn)變?yōu)檫x中。

1.28 set(item, column=None, value=None)

(1)只有item
返回指定節(jié)點(diǎn)(行)的數(shù)據(jù)字典。字典的鍵值是columns屬性定義的列的別名,而對(duì)應(yīng)的數(shù)值就是節(jié)點(diǎn)(行)的內(nèi)容。此種用法相當(dāng)于獲取指定節(jié)點(diǎn)的行內(nèi)容與列別名信息。

(2)只有item和column
返回由item和column指定的單元格的內(nèi)容。column的取值是別名。

tv.set('I002',column='all')

(3)item,column和value
如果三個(gè)參數(shù)都有值,那么會(huì)修改由item和column指定的單元格的內(nèi)容。

tv.set('I002',column='all',value='abc')

1.29 tag_bind( tagname, sequence=None, callback=None)

將tagname指定的tag與sequence指定事件綁定,回調(diào)函數(shù)由callback設(shè)定。需要注意的是,一個(gè)tag可能與多個(gè)節(jié)點(diǎn)相關(guān)。也就是說(shuō),可能會(huì)有多個(gè)item與事件綁定。

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('320x240')
tk.Label(root,text='成績(jī)表').pack()
area=('#','數(shù)學(xué)','語(yǔ)文','英語(yǔ)')
ac=('all','m','c','e')
data=[('張三','90','88','95'),
      ('李四','100','92', '90'),
      ('王二','88','90', '91')
      ]
tv=ttk.Treeview(root,columns=ac,show='headings',
                height=5)
for i in range(4):
    tv.column(ac[i],width=70,anchor='e')
    tv.heading(ac[i],text=area[i])
tv.pack()
for i in range(3):
    tv.insert('','end',values=data[i],tags=str(i))
def tb(*args):
    print(*args)
def bind():
    tv.tag_bind('1',sequence='<Button-1>',
                callback=tb)
ttk.Button(root,text='Bind',command=bind).pack()
root.mainloop()

結(jié)果:
<ButtonPress event state=Mod1 num=1 x=131 y=61>

說(shuō)明:給插入的數(shù)據(jù)每一行都設(shè)定一個(gè)tag,比如第一行的tag是'0',等等。然后通過(guò)tag_bind()方法綁定鼠標(biāo)左鍵事件。

1.30 tag_configure( tagname, option=None, **kw)

與item()方法有些類似,也是有三種功能:
(1)只有tagname
返回tagname指定tag的選項(xiàng)/屬性數(shù)據(jù)字典。比如tv.tag_configure(‘2')的返回值為:
{‘text': ‘', ‘image': ‘', ‘a(chǎn)nchor': ‘', ‘background': ‘', ‘foreground': ‘', ‘font': ‘'}

(2)tagname和option
返回tagname指定的tag中option指定的屬性值。比如option='anchor',則返回anchor屬性值。

(3)tagname和kw
對(duì)tagname指定的tag,使用kw中的參數(shù)設(shè)置有關(guān)屬性。屬性值見(jiàn)(1)中的說(shuō)明。

1.31 tag_has(tagname, item=None)

(1)只有tagname
返回所有與tagname指定的tag有關(guān)的子節(jié)點(diǎn)。

(2)tagname和item
如果item指定的子節(jié)點(diǎn)的有tagname指定的tag,則返回True,否則返回False。判斷tag是否存在。

到此這篇關(guān)于Python tkinter 樹(shù)形列表控件(Treeview)的使用方法的文章就介紹到這了,更多相關(guān)Python tkinter 樹(shù)形列表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python字符串替換示例

    python字符串替換示例

    這篇文章主要介紹了python字符串替換示例,需要的朋友可以參考下
    2014-04-04
  • Python實(shí)現(xiàn)單詞翻譯功能

    Python實(shí)現(xiàn)單詞翻譯功能

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)單詞翻譯功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • django解決訂單并發(fā)問(wèn)題【推薦】

    django解決訂單并發(fā)問(wèn)題【推薦】

    這篇文章主要介紹了django解決訂單并發(fā)問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python利用imshow制作自定義漸變填充柱狀圖(colorbar)

    Python利用imshow制作自定義漸變填充柱狀圖(colorbar)

    這篇文章主要介紹了Python利用imshow制作自定義漸變填充柱狀圖(colorbar),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • python如何查看安裝了的模塊

    python如何查看安裝了的模塊

    在本篇文章里小編給大家分享的是一篇關(guān)于python查看安裝了的模塊的方法,需要的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • 基于Pytorch SSD模型分析

    基于Pytorch SSD模型分析

    今天小編就為大家分享一篇基于Pytorch SSD模型分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • python導(dǎo)出chrome書(shū)簽到markdown文件的實(shí)例代碼

    python導(dǎo)出chrome書(shū)簽到markdown文件的實(shí)例代碼

    python導(dǎo)出chrome書(shū)簽到markdown文件,主要就是解析chrome的bookmarks文件,然后拼接成markdown格式的字符串,最后輸出到文件即可。下面給大家分享實(shí)例代碼,需要的朋友參考下
    2017-12-12
  • python如何正確的操作字符串

    python如何正確的操作字符串

    Python是一種知道如何不妨礙你編寫(xiě)程序的編程語(yǔ)言。它易于學(xué)習(xí),功能強(qiáng)大,足以構(gòu)建Web應(yīng)用程序并自動(dòng)化無(wú)聊的東西。本文是對(duì)常用字符串操作進(jìn)行了詳細(xì)的總結(jié)分析,希望對(duì)您有所幫助。
    2021-06-06
  • 跟老齊學(xué)Python之畫(huà)圈還不簡(jiǎn)單嗎?

    跟老齊學(xué)Python之畫(huà)圈還不簡(jiǎn)單嗎?

    畫(huà)圈?換一個(gè)說(shuō)法就是循環(huán)。循環(huán),是高級(jí)語(yǔ)言編程中重要的工作?,F(xiàn)實(shí)生活中,很多事情都是在循環(huán),日月更迭,斗轉(zhuǎn)星移,無(wú)不是循環(huán);王朝更迭,尋常百姓,也都是循環(huán)。
    2014-09-09
  • python_tkinter事件類型詳情

    python_tkinter事件類型詳情

    這篇文章主要介紹了python_tkinter事件詳情,文章基于python_tkinter事件相關(guān)資料分享的內(nèi)容有事件綁定函數(shù),事件對(duì)象等相關(guān)自資料,需要的小伙伴可以參考一下
    2022-03-03

最新評(píng)論