Tk treeview&scrollbar

工具类:

import tkinter as tk
from tkinter import ttk

class Application(tk.Tk):
    def __init__(self):
        super().__init__()
        self.createWidgets()

    def createWidgets(self):
        scrollBar = tk.Scrollbar(self)
        scrollBarX = tk.Scrollbar(self, orient='horizontal')
        scrollBar.pack(side='right', fill='y')
        scrollBarX.pack(side='bottom', fill='x')

        # create a treeview with scrollBar
        self.tree_date = ttk.Treeview(self, height=5, yscrollcommand=scrollBar.set, xscrollcommand=scrollBarX.set)

        # scrollBar interact treeview
        scrollBar.config(command=self.tree_date.yview)
        scrollBarX.config(command=self.tree_date.xview)

        # bind event
        self.tree_date.bind('<Button-3>', self.treeviewClick)
        self.tree_date.bind('<Double-Button-1>', self.doubleClick)
        self.tree_date.bind('<Double-Button-3>', self.tripleClick)

    def treeviewClick(self,event):  # 单击
        for item in self.tree_date.selection():
            item_text = self.tree_date.item(item,"values")
            coulumn = int(self.tree_date.identify_column(event.x).replace("#",""))
            text = item_text[coulumn-1]
            self.clipboard_clear()
            self.clipboard_append(text)
    def doubleClick(self,event):  # 双击
        text = ''
        for item in self.tree_date.selection():
            for val in self.tree_date.item(item,"values"):
                text += val
                text += '\t'
            text += '\n'
            self.clipboard_clear()
            self.clipboard_append(text)
    def tripleClick(self, event):  # 右键双击
        text = ''
        for i in self.tree_date.get_children():
            for val in self.tree_date.item(i,"values"):
                text += val
                text += '\t'
            text += '\n'
        self.clipboard_clear()
        self.clipboard_append(text)

调用:

from Test.ScrollTable import Application


def insert_date(tree_date):
    tree_date['columns'] = ['name', 'age', 'weight', 'number']
    tree_date.pack()
    tree_date.column('name', width=100)
    tree_date.column('age', width=100)
    tree_date.column('weight', width=100)
    tree_date.column('number', width=100)
    tree_date.heading('name', text='姓名')
    tree_date.heading('age', text='年龄')
    tree_date.heading('weight', text='体重')
    tree_date.heading('number', text='工号')
    tree_date['show'] = 'headings'  # delete first default column
    tree_date.insert('', 0, text='date1', values=('大豆', 21, '60kg', '3121211034'))
    tree_date.insert('', 1, text='date2', values=('花生', 21, '54kg', '3121211033'))
    tree_date.insert('', 2, text='date3', values=('玉米', 21, '65kg', '3121211023'))
    tree_date.insert('', 3, text='date4', values=('土豆', 21, '34kg', '3121211053'))
    tree_date.insert('', 4, text='date5', values=('番茄', 21, '65kg', '3121211063'))
    tree_date.insert('', 6, text='date6', values=('高粱', 21, '64kg', '3121211073'))

if __name__ == '__main__':
    app = Application()
    insert_date(app.tree_date)
    app.mainloop()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值