1.建立Treeview
from tkinter import * from tkinter.ttk import * root = Tk() # 建立Treeview columns = ( ('ID', 50), ('S_ID', 50), ('S_NAME', 120), ('B_NAME', 120), ('Date_Taken', 100), ('Due_Date', 100), ('Date_Returned', 100), ('Status', 50), ) treeview = Treeview(root, height=20, columns=[x[0] for x in columns], show='headings') for col, width in columns: treeview.heading(col, text=col) treeview.column(col, width=width, anchor=CENTER) treeview.place(x=50,y=50) root.geometry("1000x800+10+10") root.mainloop()
2.在root上加一个Entry
from tkinter import * from tkinter.ttk import * root = Tk() root.geometry("1000x800+10+10") # 建立treeview1 columns = ( ('ID', 50), ('S_ID', 50), ('S_NAME', 120), ('B_NAME', 120), ('Date_Taken', 100), ('Due_Date', 100), ('Date_Returned', 100), ('Status', 50), ) treeview1 = Treeview(root, height=20, columns=[x[0] for x in columns], show='headings') for col, width in columns: treeview1.heading(col, text=col) treeview1.column(col, width=width, anchor=CENTER) treeview1.place(x=50,y=50) #增加输入框 en1=Entry(root,width=10) en1.place(x=50,y=10) def treeitemadd(event): print(event.keysym) #在treeview1增加行 en1txt = event.widget.get() values=(en1txt,en1txt,en1txt,en1txt) treeview1.insert("",'end',values=values) treeview1.update() # 绑定keypress-Return事件到Entry上 en1.bind('<KeyPress-Return>', treeitemadd) root.mainloop()
3.获取treeview的一些属性
from tkinter import * from tkinter.ttk import * root = Tk() root.geometry("1000x800+10+10") # 添加一个菜单 m = Menu(root) root.config(menu=m) # 添加菜单下拉列表项 file_menu = Menu(m, tearoff=False) m.add_cascade(label="菜单", menu=file_menu) file_menu.add_command(label="打开", ) # 建立treeview1 columns = ( ('ID', 50), ('S_ID', 50), ('S_NAME', 120), ('B_NAME', 120), ('Date_Taken', 100), ('Due_Date', 100), ('Date_Returned', 100), ('Status', 50), ) treeview1 = Treeview(root, height=20, columns=[x[0] for x in columns], show='headings') for col, width in columns: treeview1.heading(col, text=col+"好") treeview1.column(col, width=width, anchor=CENTER) treeview1.place(x=50,y=50) # Create a Scrollbar scrollbar = Scrollbar(root, orient="vertical", command=treeview1.yview) # Configure the Treeview to use the scrollbar treeview1.configure(yscrollcommand=scrollbar.set) # Place the scrollbar on the right side of the Treeview scrollbar.set(0.5,0) scrollbar.place(x=800,y=200) treeview2 = Treeview(root, height=20, columns=[x[0] for x in columns], show='headings') treeview2.column(0,width=200) #增加输入框 en1=Entry(root,width=10) en1.place(x=50,y=10) def finddatashow(event): treeview2.place(x=50,y=350) def finddatahide(event): treeview2.place_forget() def treeitemadd(event): #在treeview1增加行 en1txt = event.widget.get() values=(en1txt,en1txt,en1txt,en1txt) #treeview1.insert("",'end',values=values) for i in range(5): treeview1.insert("",'end',values=(i,)) treeview1.update() def treedoubleclick(event): print( "标题列:",treeview1["column"]); widg = event.widget # treeview句柄 print(widg) itemsel = widg.selection() #选择的行 print('选定行:',itemsel) column= treeview1.identify_column(event.x)# 列 row = treeview1.identify_row(event.y) # 行 print("列,行:",column,row) print("========",treeview1.column(7)) for col in treeview1["column"]: print(col); if row: #防止treeview为空而出错 cn = int(str(column).replace('#',''),base=16) #注意是设置16进制,如果按默认10进制则会有bug rn = int(str(row).replace('I',''),base=16) print(cn,rn) print(type(column)) #修改选定行 treeview1.set(itemsel, column=0, value="222") #修改第1行 treeview1.set(item = ('I001',), column=0, value="333") #获取所有行 itemall = treeview1.get_children() print(itemall) #获取行数 print("共几行:",len(itemall)) #获取第二个表头 col2 = treeview1.heading(2) print("表头2:",col2) #删除所有行 #treeview1.delete(*itemall) treeview1.update() #没这一行好像也行 # 绑定keypress-Return事件到Entry上 en1.bind('<KeyPress-Return>', treeitemadd) en1.bind('<FocusIn>',finddatashow) en1.bind('<FocusOut>',finddatahide) # 绑定<Double-1>鼠标双击事件到Treeview上 treeview1.bind('<Double-1>', treedoubleclick) root.mainloop()
标签:treeview1,示例,python,50,column,treeview,root,col,columns From: https://www.cnblogs.com/pu369/p/17838229.html