Python Tkinter Treeview組件核心方法詳解與實(shí)戰(zhàn)
在Python Tkinter模塊中,Treeview是在應(yīng)用程序中實(shí)現(xiàn)樹(shù)狀結(jié)構(gòu)或表格形式的數(shù)據(jù)。它不僅能展示層級(jí)結(jié)構(gòu),還支持增刪改查等操作。本文將講述Treeview的核心方法。
一、Treeview的基本概念
Treeview是Tkinter中 ttk 模塊的一個(gè)控件,用以展示樹(shù)狀結(jié)構(gòu)或表格數(shù)據(jù)。Treeview 在 Tk 中沒(méi)有控件,所以必須導(dǎo)入 tkinter.ttk() 。
二、導(dǎo)入tkinter,ttk
注:由于tkinter是Python IDLE 自帶的模塊,因此無(wú)需pip安裝。
from tkinter import * from tkinter import ttk
三、Treeview 的基本使用
1.創(chuàng)建 Treeview 對(duì)象
from tkinter import *
from tkinter import ttk
# 創(chuàng)建主窗口
root = Tk()
root.title("Treeview Example")
root.geometry("300x300")
# 創(chuàng)建 Treeview 對(duì)象
tree = ttk.Treeview(root, column=("mid", "name" , "salary"),show="tree headings")
tree.pack(fill="both", expand = True)
#設(shè)置列標(biāo)題
tree.heading("mid", text = "ID編號(hào)")
tree.heading("name" , text = "姓名")
tree.heading("salary" , text = "工資")
#設(shè)置列寬
tree.column("mid", width = 30, anchor = "center")
tree.column("name", width=80,anchor = "center")
tree.column("salary", width=30,anchor = "center")
root.mainloop()•運(yùn)行結(jié)果:

2.添加數(shù)據(jù)
#添加根節(jié)點(diǎn),#0是Treeview預(yù)留的第一空列的標(biāo)識(shí),用戶可以設(shè)置它
tree.column("#0", width=20, anchor="w")
level_a = tree.insert("", "end",text="員工")
#添加子節(jié)點(diǎn)
tree.insert(level_a, "end", values=("01","Tom",25000))
tree.insert(level_a, "end", values=("02","Jerry",10000))
tree.insert(level_a, "end",values=("03","James",16000))•在添加節(jié)點(diǎn)是,使用的是 insert() 方法,該方法有3個(gè)參數(shù):
使用的方法: insert(parent, position, text)
- parent:空字符串表示新項(xiàng)是父項(xiàng)。""
- position:"end"指示小組件將項(xiàng)目放置在樹(shù)的末尾。
- text:指定項(xiàng)目的標(biāo)簽。
•效果展示:

3.表格制作
由于Tkinter沒(méi)有直接開(kāi)發(fā)表格的控件,因此Treeview可以很好的進(jìn)行表格開(kāi)發(fā)。
from tkinter import *
from tkinter import ttk
# 創(chuàng)建主窗口
root = Tk()
root.title("Treeview Example")
root.geometry("300x300")
# 創(chuàng)建 Treeview 對(duì)象
tree = ttk.Treeview(root, column=("id", "name" , "academic_report"),show="headings")
tree.pack(fill="both", expand = True)
#設(shè)置列標(biāo)題
tree.heading("id", text = "學(xué)號(hào)")
tree.heading("name" , text = "姓名")
tree.heading("academic_report" , text = "成績(jī)")
#設(shè)置列寬
tree.column("id", width = 30, anchor = "center")
tree.column("name", width=80,anchor = "center")
tree.column("academic_report", width=30,anchor = "center")
#添加節(jié)點(diǎn),每個(gè)節(jié)點(diǎn)都是父節(jié)點(diǎn)
tree.insert('', "end", values=("01","Tom",702))
tree.insert('', "end", values=("02","Jerry",673))
tree.insert('', "end",values=("03","James",624))
tree.insert("","end",values=("04","Alice",608))
root.mainloop()制作表格與普通樹(shù)狀結(jié)構(gòu)的不同之處:
- ttk,Treeview() 中的 show="tree headings" 改為 show = "headings" ,這將忽略 Treeview 創(chuàng)建的第一空列(即 #0 );
- 調(diào)用 tree.insert() 方法時(shí),第一個(gè)參數(shù)一直是 "" ,因此表格中每一項(xiàng)都是父項(xiàng),無(wú)子項(xiàng)。
運(yùn)行結(jié)果:

三、Treeview 的高級(jí)功能
Treeview 支持事件綁定,動(dòng)態(tài)更新數(shù)據(jù)等功能。
1.事件綁定
Treeview 支持事件綁定從而實(shí)現(xiàn)與用戶的交互。例如,綁定雙擊事件以顯示選中的數(shù)據(jù)。( 注:代碼為表格制作的后續(xù) )
#綁定雙擊事件,以顯示所選中的數(shù)據(jù)
def show_data(event):
# 獲取選中的數(shù)據(jù)
selected_item = tree.selection()[0]
data = tree.item(selected_item, "values")
print(f"學(xué)號(hào):{data[0]}\n姓名:{data[1]}\n成績(jī){data[2]}")
print("-"*30)
tree.bind("<Double-1>", show_data)運(yùn)行結(jié)果:

2.動(dòng)態(tài)更新數(shù)據(jù)
Treeview 支持動(dòng)態(tài)更新數(shù)據(jù)。例如,刪除某一行或修改某一行:
# 獲取數(shù)據(jù)編號(hào) ---> tree.get_children()方法,返回元組對(duì)象 child_list = tree.get_children() print(child_list) # 刪除對(duì)象 delete_item = child_list[3] tree.delete(delete_item)
刪除結(jié)果顯示:

修改某一行,可以這樣操作:
# 修改數(shù)據(jù)
modified_item = child_list[2]
tree.item(modified_item, values = ("05", "Jack", 523))運(yùn)行結(jié)果:

四、完整源碼分享
1.樹(shù)狀員工列表
from tkinter import *
from tkinter import ttk
# 創(chuàng)建主窗口
root = Tk()
root.title("Treeview Example")
root.geometry("300x300")
# 創(chuàng)建 Treeview 對(duì)象
tree = ttk.Treeview(root, column=("mid", "name" , "salary"),show="tree headings")
tree.pack(fill="both", expand = True)
#設(shè)置列標(biāo)題
tree.heading("mid", text = "ID編號(hào)")
tree.heading("name" , text = "姓名")
tree.heading("salary" , text = "工資")
#設(shè)置列寬
tree.column("mid", width = 30, anchor = "center")
tree.column("name", width=80,anchor = "center")
tree.column("salary", width=30,anchor = "center")
#添加根節(jié)點(diǎn),#0是Treeview預(yù)留的第一空列的標(biāo)識(shí),用戶可以設(shè)置它
tree.column("#0", width=20, anchor="w")
level_a = tree.insert("", "end",text="員工")
#添加子節(jié)點(diǎn)
tree.insert(level_a, "end", values=("01","Tom",25000))
tree.insert(level_a, "end", values=("02","Jerry",10000))
tree.insert(level_a, "end",values=("03","James",16000))
root.mainloop()2.學(xué)生成績(jī)表格顯示
from tkinter import *
from tkinter import ttk
# 創(chuàng)建主窗口
root = Tk()
root.title("Treeview Example")
root.geometry("300x300")
# 創(chuàng)建 Treeview 對(duì)象
tree = ttk.Treeview(root, column=("id", "name" , "academic_report"),show="headings")
tree.pack(fill="both", expand = True)
#設(shè)置列標(biāo)題
tree.heading("id", text = "學(xué)號(hào)")
tree.heading("name" , text = "姓名")
tree.heading("academic_report" , text = "成績(jī)")
#設(shè)置列寬
tree.column("id", width = 30, anchor = "center")
tree.column("name", width=80,anchor = "center")
tree.column("academic_report", width=30,anchor = "center")
#添加節(jié)點(diǎn),每個(gè)節(jié)點(diǎn)都是父節(jié)點(diǎn)
tree.insert('', "end", values=("01","Tom",702))
tree.insert('', "end", values=("02","Jerry",673))
tree.insert('', "end",values=("03","James",624))
tree.insert("","end",values=("04","Alice",608))
#綁定雙擊事件,以顯示所選中的數(shù)據(jù)
def show_data(event):
# 獲取選中的數(shù)據(jù)
selected_item = tree.selection()[0]
data = tree.item(selected_item, "values")
print(f"學(xué)號(hào):{data[0]}\n姓名:{data[1]}\n成績(jī){data[2]}")
print("-"*30)
tree.bind("<Double-1>", show_data)
# 獲取數(shù)據(jù)編號(hào) ---> tree.get_children()方法,返回元組對(duì)象
child_list = tree.get_children()
print(child_list)
# 刪除數(shù)據(jù)
delete_item = child_list[3]
tree.delete(delete_item)
# 修改數(shù)據(jù)
modified_item = child_list[2]
tree.item(modified_item, values = ("05", "Jack", 523))
root.mainloop()通過(guò)本文的講解,您應(yīng)該能夠掌握 Treeview 的基本使用方法及其在實(shí)際開(kāi)發(fā)中的應(yīng)用場(chǎng)景。希望本文對(duì)您有所幫助!
到此這篇關(guān)于Python Tkinter Treeview組件詳解與實(shí)戰(zhàn)的文章就介紹到這了,更多相關(guān)Python Tkinter Treeview組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python不同目錄間進(jìn)行模塊調(diào)用的實(shí)現(xiàn)方法
這篇文章主要介紹了Python不同目錄間進(jìn)行模塊調(diào)用的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
python opencv攝像頭的簡(jiǎn)單應(yīng)用
這篇文章主要為大家詳細(xì)介紹了python opencv攝像頭的簡(jiǎn)單應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
linux平臺(tái)使用Python制作BT種子并獲取BT種子信息的方法
這篇文章主要介紹了linux平臺(tái)使用Python制作BT種子并獲取BT種子信息的方法,結(jié)合實(shí)例形式詳細(xì)分析了Python BT模塊的安裝及針對(duì)BT種子文件的相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
Python?opencv應(yīng)用實(shí)現(xiàn)圖片切分操作示例
這篇文章主要為大家介紹了Python?opencv應(yīng)用實(shí)現(xiàn)圖片切分的操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Python輕松實(shí)現(xiàn)將數(shù)據(jù)庫(kù)數(shù)據(jù)一鍵導(dǎo)出到Excel
在日常工作中,我們經(jīng)常需要將數(shù)據(jù)庫(kù)中的數(shù)據(jù)導(dǎo)出為 Excel 文件,以便進(jìn)行數(shù)據(jù)分析或業(yè)務(wù)匯報(bào),本文將介紹如何僅使用 Python 內(nèi)置庫(kù) + 免費(fèi) Excel 處理庫(kù),實(shí)現(xiàn)將數(shù)據(jù)庫(kù)所有表批量導(dǎo)出到一個(gè) Excel 文件,每個(gè)表對(duì)應(yīng)一個(gè)獨(dú)立工作表2026-03-03
python實(shí)現(xiàn)FFT快速傅立葉變換算法案例
FFT(快速傅里葉變換)是計(jì)算DFT及其逆變換的一種算法,其基本思想是利用DFT的對(duì)稱(chēng)性和周期性,通過(guò)分而治之的策略將DFT分解為更小的DFT,從而降低計(jì)算復(fù)雜度,FFT的算法步驟包括選擇分解、重新排序、蝶形運(yùn)算和逐層計(jì)算,在Python中2024-10-10
Python操作MySQL數(shù)據(jù)庫(kù)的三種方法總結(jié)
下面小編就為大家分享一篇Python操作MySQL數(shù)據(jù)庫(kù)的三種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
樹(shù)莓派中python獲取GY-85九軸模塊信息示例
本文內(nèi)容是樹(shù)莓派中python獲取GY-85九軸模塊信息的示例,這里使用Python的curses包開(kāi)發(fā)cli窗口程序,用來(lái)實(shí)時(shí)刷新傳感器的讀數(shù),下面看代碼2013-12-12
python統(tǒng)計(jì)多維數(shù)組的行數(shù)和列數(shù)實(shí)例
今天小編就為大家分享一篇python統(tǒng)計(jì)多維數(shù)組的行數(shù)和列數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06

