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

2.添加數(shù)據(jù)
#添加根節(jié)點,#0是Treeview預(yù)留的第一空列的標識,用戶可以設(shè)置它
tree.column("#0", width=20, anchor="w")
level_a = tree.insert("", "end",text="員工")
#添加子節(jié)點
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é)點是,使用的是 insert() 方法,該方法有3個參數(shù):
使用的方法: insert(parent, position, text)
- parent:空字符串表示新項是父項。""
- position:"end"指示小組件將項目放置在樹的末尾。
- text:指定項目的標簽。
•效果展示:

3.表格制作
由于Tkinter沒有直接開發(fā)表格的控件,因此Treeview可以很好的進行表格開發(fā)。
from tkinter import *
from tkinter import ttk
# 創(chuàng)建主窗口
root = Tk()
root.title("Treeview Example")
root.geometry("300x300")
# 創(chuàng)建 Treeview 對象
tree = ttk.Treeview(root, column=("id", "name" , "academic_report"),show="headings")
tree.pack(fill="both", expand = True)
#設(shè)置列標題
tree.heading("id", text = "學(xué)號")
tree.heading("name" , text = "姓名")
tree.heading("academic_report" , text = "成績")
#設(shè)置列寬
tree.column("id", width = 30, anchor = "center")
tree.column("name", width=80,anchor = "center")
tree.column("academic_report", width=30,anchor = "center")
#添加節(jié)點,每個節(jié)點都是父節(jié)點
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()制作表格與普通樹狀結(jié)構(gòu)的不同之處:
- ttk,Treeview() 中的 show="tree headings" 改為 show = "headings" ,這將忽略 Treeview 創(chuàng)建的第一空列(即 #0 );
- 調(diào)用 tree.insert() 方法時,第一個參數(shù)一直是 "" ,因此表格中每一項都是父項,無子項。
運行結(jié)果:

三、Treeview 的高級功能
Treeview 支持事件綁定,動態(tài)更新數(shù)據(jù)等功能。
1.事件綁定
Treeview 支持事件綁定從而實現(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é)號:{data[0]}\n姓名:{data[1]}\n成績{data[2]}")
print("-"*30)
tree.bind("<Double-1>", show_data)運行結(jié)果:

2.動態(tài)更新數(shù)據(jù)
Treeview 支持動態(tài)更新數(shù)據(jù)。例如,刪除某一行或修改某一行:
# 獲取數(shù)據(jù)編號 ---> tree.get_children()方法,返回元組對象 child_list = tree.get_children() print(child_list) # 刪除對象 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))運行結(jié)果:

四、完整源碼分享
1.樹狀員工列表
from tkinter import *
from tkinter import ttk
# 創(chuàng)建主窗口
root = Tk()
root.title("Treeview Example")
root.geometry("300x300")
# 創(chuàng)建 Treeview 對象
tree = ttk.Treeview(root, column=("mid", "name" , "salary"),show="tree headings")
tree.pack(fill="both", expand = True)
#設(shè)置列標題
tree.heading("mid", text = "ID編號")
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é)點,#0是Treeview預(yù)留的第一空列的標識,用戶可以設(shè)置它
tree.column("#0", width=20, anchor="w")
level_a = tree.insert("", "end",text="員工")
#添加子節(jié)點
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é)生成績表格顯示
from tkinter import *
from tkinter import ttk
# 創(chuàng)建主窗口
root = Tk()
root.title("Treeview Example")
root.geometry("300x300")
# 創(chuàng)建 Treeview 對象
tree = ttk.Treeview(root, column=("id", "name" , "academic_report"),show="headings")
tree.pack(fill="both", expand = True)
#設(shè)置列標題
tree.heading("id", text = "學(xué)號")
tree.heading("name" , text = "姓名")
tree.heading("academic_report" , text = "成績")
#設(shè)置列寬
tree.column("id", width = 30, anchor = "center")
tree.column("name", width=80,anchor = "center")
tree.column("academic_report", width=30,anchor = "center")
#添加節(jié)點,每個節(jié)點都是父節(jié)點
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é)號:{data[0]}\n姓名:{data[1]}\n成績{data[2]}")
print("-"*30)
tree.bind("<Double-1>", show_data)
# 獲取數(shù)據(jù)編號 ---> tree.get_children()方法,返回元組對象
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()通過本文的講解,您應(yīng)該能夠掌握 Treeview 的基本使用方法及其在實際開發(fā)中的應(yīng)用場景。希望本文對您有所幫助!
到此這篇關(guān)于Python Tkinter Treeview組件詳解與實戰(zhàn)的文章就介紹到這了,更多相關(guān)Python Tkinter Treeview組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python不同目錄間進行模塊調(diào)用的實現(xiàn)方法
這篇文章主要介紹了Python不同目錄間進行模塊調(diào)用的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
linux平臺使用Python制作BT種子并獲取BT種子信息的方法
這篇文章主要介紹了linux平臺使用Python制作BT種子并獲取BT種子信息的方法,結(jié)合實例形式詳細分析了Python BT模塊的安裝及針對BT種子文件的相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
Python?opencv應(yīng)用實現(xiàn)圖片切分操作示例
這篇文章主要為大家介紹了Python?opencv應(yīng)用實現(xiàn)圖片切分的操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
Python輕松實現(xiàn)將數(shù)據(jù)庫數(shù)據(jù)一鍵導(dǎo)出到Excel
在日常工作中,我們經(jīng)常需要將數(shù)據(jù)庫中的數(shù)據(jù)導(dǎo)出為 Excel 文件,以便進行數(shù)據(jù)分析或業(yè)務(wù)匯報,本文將介紹如何僅使用 Python 內(nèi)置庫 + 免費 Excel 處理庫,實現(xiàn)將數(shù)據(jù)庫所有表批量導(dǎo)出到一個 Excel 文件,每個表對應(yīng)一個獨立工作表2026-03-03
Python操作MySQL數(shù)據(jù)庫的三種方法總結(jié)
下面小編就為大家分享一篇Python操作MySQL數(shù)據(jù)庫的三種方法總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
python統(tǒng)計多維數(shù)組的行數(shù)和列數(shù)實例
今天小編就為大家分享一篇python統(tǒng)計多維數(shù)組的行數(shù)和列數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06

