python ttk Treeview的插入、清空、各種點擊事件、獲取條目值、標題單擊排序


文章轉自:   https://blog.csdn.net/sinat_27382047/article/details/80161637

 

插入方法:

import tkinter
from tkinter import ttk # 導入內部包

li = ['王記','12','男']
root = tkinter.Tk()
root.title('測試')
tree = ttk.Treeview(root,columns=['1','2','3'],show='headings')
tree.column('1',width=100,anchor='center')
tree.column('2',width=100,anchor='center')
tree.column('3',width=100,anchor='center')
tree.heading('1',text='姓名')
tree.heading('2',text='學號')
tree.heading('3',text='性別')
tree.insert('','end',values=li)
tree.grid()

root.mainloop()

  

代碼來自博客園提問:https://q.cnblogs.com/q/105829/#a_226277

 

tree.insert('','end',values=li)

 

 

 

關於“首列很寬”的問題:https://segmentfault.com/q/1010000004206667?sort=created

 

設置show屬性為 headings 即可隱藏首列。

 
ttk.Treeview(frame,height=18, show="headings", columns=('a','b','c','d','e','f'))

 

 

配上一個還可以的綜合一點的例子https://www.cnblogs.com/Tommy-Yu/p/4156014.html

 

清空方法:

參考stackoverflow的提問https://stackoverflow.com/questions/32511843/delete-and-edit-items-in-treeview-tkinter

代碼是對上面的代碼添加:

import tkinter
from tkinter import ttk  # 導入內部包
 
li = ['王記','12','']
root = tkinter.Tk()
root.title('測試')
tree = ttk.Treeview(root,columns=['1','2','3'],show='headings')
tree.column('1',width=100,anchor='center')
tree.column('2',width=100,anchor='center')
tree.column('3',width=100,anchor='center')
tree.heading('1',text='姓名')
tree.heading('2',text='學號')
tree.heading('3',text='性別')
tree.insert('','end',values=li)
tree.grid()
 
def delButton(tree):
    x=tree.get_children()
    for item in x:
        tree.delete(item)
 
delButton(tree)
 
root.mainloop()

 

(同樣是為了省事……這個就是我在博客園的那個提問的回答)

 

另一種方法:來自很有用但是雜的鏈接:https://www.cnblogs.com/wumac/p/5816764.html

 

items = your_treeview.get_children()
 
[your_treeview.delete(item) for item in items]

 

 

獲取條目值

同樣沒見到有人問,stackoverflow上才找到的方法

https://stackoverflow.com/questions/34849035/how-to-get-the-value-of-a-selected-treeview-item

import tkinter
from tkinter import ttk  # 導入內部包
 
li = ['王記','12','']
root = tkinter.Tk()
root.title('測試')
tree = ttk.Treeview(root,columns=['1','2','3'],show='headings')
tree.column('1',width=100,anchor='center')
tree.column('2',width=100,anchor='center')
tree.column('3',width=100,anchor='center')
tree.heading('1',text='姓名')
tree.heading('2',text='學號')
tree.heading('3',text='性別')
tree.insert('','end',values=li)
tree.grid()
 
 
def treeviewClick(event):#單擊
    print ('單擊')
    for item in tree.selection():
        item_text = tree.item(item,"values")
        print(item_text[0])#輸出所選行的第一列的值
 
tree.bind('<ButtonRelease-1>', treeviewClick)#綁定單擊離開事件===========
 
root.mainloop()

 

 

見函數treeview里的內容

 

有用的鏈接:https://www.cnblogs.com/Tommy-Yu/p/4156014.html

 

各種點擊事件

事件一覽表
事件 代碼 備注
鼠標左鍵單擊按下 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  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

來自:https://www.cnblogs.com/wumac/p/5842393.html

用法:

tree.bind('<ButtonRelease-1>', treeviewClick)#綁定單擊離開事件===========

 

 
 

在bind方法里的'<>'添上相應的事件代碼就好了,后面的treeview就是我定義的函數

 

標題點擊排序

https://blog.csdn.net/mikewolfli/article/details/51393933(強烈懷疑此鏈接是沒注明來源,來自下面的鏈接的代碼)

https://stackoverflow.com/questions/22032152/python-ttk-treeview-sort-numbers(不按默認桶排序的方法)

https://stackoverflow.com/questions/1966929/tk-treeview-column-sort(python3可用的見此鏈接最下面)

試驗可運行代碼

import random
from tkinter import ttk
from tkinter import *
 
root = Tk()     # 初始曠的聲明
columns=("a","b","c")
treeview=ttk.Treeview(root,height=18,show="headings",columns=columns )#表格 
 
treeview.column('a', width=50, anchor='center') 
treeview.column('b', width=100, anchor='center') 
treeview.column('c', width=80, anchor='center')
treeview.heading('a', text='列1')
treeview.heading('b', text='列2')
treeview.heading('c', text='列3')
treeview.pack(side=LEFT,fill=BOTH)
for i in range(10):
    treeview.insert('',i,values=(str(random.randint(0,9)),str(random.randint(0,9)),str(random.randint(0,9))))
 
 
def treeview_sort_column(tv, col, reverse):#Treeview、列名、排列方式
    l = [(tv.set(k, col), k) for k in tv.get_children('')]
    print(tv.get_children(''))
    l.sort(reverse=reverse)#排序方式
    # rearrange items in sorted positions
    for index, (val, k) in enumerate(l):#根據排序后索引移動
        tv.move(k, '', index)
        print(k)
    tv.heading(col, command=lambda: treeview_sort_column(tv, col, not reverse))#重寫標題,使之成為再點倒序的標題
 
'''
#莫名其妙????寫循環的話只有最后一列管用,看論壇說的好像是python2.7管用
for col in columns:
    treeview.heading(col, text=col, command=lambda: treeview_sort_column(treeview, col, False))
'''
 
'''
#基本用法(上邊注釋的只有最后一列管用、索性手工試驗一下可用性,證實可行)
treeview.heading('a', text='123', command=lambda: treeview_sort_column(tree, 'a', False))#重建標題,添加控件排序方法
treeview.heading('b', text='111', command=lambda: treeview_sort_column(tree, 'b', False))#重建標題,添加控件排序方法
treeview.heading('c', text='223', command=lambda: treeview_sort_column(tree, 'c', False))#重建標題,添加控件排序方法
'''
 
#這個代碼對於python3就管用了
for col in columns:#給所有標題加(循環上邊的“手工”)
    treeview.heading(col, text=col, command=lambda _col=col: treeview_sort_column(treeview, _col, False))
 
root.mainloop()#進入消息循環

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM