首页 > 编程语言 >python tkinter treeview 操作示例

python tkinter treeview 操作示例

时间:2023-11-22 17:55:50浏览次数:37  
标签:treeview1 示例 python 50 column treeview root col columns

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

相关文章

  • python字典中删除键值的方法
    一、pop()方法删除keyPython字典是一种无序的映射数据类型,通过键值对的形式进行存储,可以使用键来快速找到对应的值。在某些情况下,我们可能需要在字典中删除某个键,这时候就可以使用Python字典提供的pop()方法。pop()方法用于删除字典中指定的键,并返回该键对应的值。使用该方法时......
  • python中四种方法提升数据处理的速度
    在数据科学计算、机器学习、以及深度学习领域,Python是最受欢迎的语言。Python在数据科学领域,有非常丰富的包可以选择,numpy、scipy、pandas、scikit-learn、matplotlib。但这些库都仅仅受限于单机运算,当数据量很大时,比如50GB甚至500GB的数据集,这些库的处理能力都显得捉襟见肘,打......
  • python中常见函数
    filter,reduce,和map是Python中用于对集合数据进行处理和转换的内置函数。它们分别用于筛选、归约和映射集合中的元素。filter函数:filter(function,iterable)用于筛选集合中的元素。它接受一个函数function和一个可迭代的对象iterable,并返回一个包含iterable中满足......
  • python多线程中一种错误的写法
    直接先上错误代码:importmultiprocessingdeffirst_way():init=3defprocess_function(item):result=item*initreturnresultdata=[1,2,3,4,5,6,7,8,9,10]pool=multiprocessing.Pool(processes=4)#创建一个......
  • Python基础知识
    一、先置知识1、标识符标识符由字母、数字、下划线组成。所有标识符可以包括英文、数字以及下划线(_),但不能以数字开头。标识符是区分大小写的。以下划线开头的标识符是有特殊意义的。以单下划线开头_foo的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用**fr......
  • 如何在Python中向一个集合添加值
    用Set.add()函数向一个集合只添加一个值从数学上讲,集合是一个在逻辑上有联系的不同对象的集合。在Python中,集合是一个内置的数据类型,它是无索引的和不可变的。这意味着我们可以通过一些特定的索引来访问集合项,而且我们不能修改集合内的现有数据。我们可以通过在Python中创建一个......
  • 使用Python协程并发测试cdn响应速度
    代码干净清爽才能看着赏心悦目:#!/usr/bin/envpython3.11importtimefromcontextlibimportcontextmanagerfromenumimportStrEnumimportanyioimporthttpx@contextmanagerdeftimeit(msg:str):start=time.time()yieldcost=time.time()-sta......
  • python wordcloud生成词云
    #!/usr/bin/envpython#coding:utf-8#pipinstallwordcloud#pipinstallmatplotlibimportwordcloudimportmatplotlib.pyplotaspltimportnumpyasnpfromPILimportImagetext="""给你一瓶魔法药水喝下去就不需要氧气给你一瓶魔法药水喝下去就不怕身体......
  • 基于go-zero的api网关示例
    以下是基于go-zero框架的API网关示例。这个示例包括一个简单的API网关,它接收HTTP请求,将请求路由到不同的服务,并返回响应。创建项目首先,在go-zero仓库中创建一个新的目录:mkdirapi-gatewaycdapi-gateway创建Go文件在api-gateway目录下创建以下三个文件:c......
  • Python全局解释器锁GIL机制
    全局解释器锁GlobalInterpreterLock,CPython在解释器级别的一把锁,叫GIL全局解释器锁。程序编译成字节码,程序想跑多线程,但是GIL保证CPython进程中,同一时刻只能有一个线程执行字节码。所以,哪怕是在多CPU的情况下,即使每个线程恰好调度到了每个CPU上,有了这把大锁,同时只能有一个CPU......