python: Treeview Pagination

 

# encoding: utf-8
# 版權所有 2024 ©塗聚文有限公司
# 許可資訊查看:言語成了邀功的功臣,還需要行爲每日來值班嗎?
# 描述:   Treeview Pagination
# Author    : geovindu,Geovin Du 塗聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# Datetime  : 2024/12/02 20:00
# User      : geovindu
# Product   : PyCharm
# Project   : IctGame
# File      : ttkbootstrap.py
# explain   : 學習

import tkinter as tk
from tkinter import ttk


def loaddata(page):
    """

    :param page:
    :return:
    """
    global currentpage
    global maxpage
    global tatalpage

    table = [
        (1,"geovindu1", "Geovin", "geovindu@example.com"),
        (2,"geovindu2", "Geovin", "geovindu@example.com"),
        (3,"geovindu3", "Geovin", "geovindu@example.com"),
        (4,"geovindu4", "Geovin", "geovindu@example.com"),
        (5,"geovindu5", "Geovin", "geovindu@example.com"),
        (6,"geovindu6", "Geovin", "geovindu@example.com"),
        (7,"geovindu7", "Geovin", "geovindu@example.com"),
        (8,"geovindu8", "Geovin", "geovindu@example.com"),
        (9,"geovindu9", "Geovin", "geovindu@example.com"),
        (10,"geovindu10", "Geovin", "geovindu@example.com"),
        (11,"geovindu11", "Geovin", "geovindu@example.com"),
        (12,"geovindu12", "Geovin", "geovindu@example.com"),
        (13,"geovindu13", "Geovin", "geovindu@example.com"),
        (14,"geovindu14", "Geovin", "geovindu.Geovin@example.com"),
        (15,"geovindu15", "Geovin", "geovindu@example.com"),
        (16,"geovindu16", "Geovin", "geovindu@example.com"),
        (17,"geovindu17", "Geovin", "geovindu.Geovin@example.com"),
        (18,"geovindu18", "Geovin", "geovindu@example.com"),
        (19,"geovindu19", "Geovin", "geovindu@example.com"),
        (20,"geovindu20", "Geovin", "geovindu.Geovin@example.com"),
        (21,"geovindu21", "Geovin", "geovindu@example.com"),
        (22,"geovindu22", "Geovin", "geovindu@example.com"),
        (23,"geovindu23", "Geovin", "geovindu.Geovin@example.com"),
        (24,"geovindu24", "Geovin", "geovindu@example.com"),
        (25,"geovindu25", "Geovin", "geovindu@example.com"),
        (26,"geovindu26", "Geovin", "geovindu.Geovin@example.com"),
        (27,"geovindu27", "Geovin", "geovindu@example.com"),
        (28,"geovindu28", "Geovin", "geovindu@example.com"),
        (29,"geovindu29", "Geovin", "geovindu.Geovin@example.com"),
        (30,"geovindu30", "Geovin", "geovindu@example.com")
    ]
    # 计算每页显示
    pagesize = 10
    total = len(table)

    startindex = (page - 1) * pagesize
    endindex = startindex + pagesize
    # 计算总共多少页
    if total % pagesize == 0:
        tatalpage = total / pagesize
    else:
        tatalpage = total / pagesize + 1

    totallabel.config(text=f"/{int(tatalpage)}")
    # 返回当前页的数据
    return table[startindex:endindex]


def updatetreeview(treeview, page):
    """
    消空Treeview中的所有硕
    :param treeview:
    :param page:
    :return:
    """
    treeview.delete(*treeview.get_children())
    print(page)
    # 并 加载当煎页的数据
    table = loaddata(page)
    # 将数据添加Treeview中
    for item in table:
        treeview.insert("", "end", values=item)


def prevpage(treeview, pagelabel):
    """

    :param treeview:
    :param pagelabel:
    :return:
    """
    ll = pagelabel.cget("text").replace('Page ', '')  # page 2
    print('prev', ll)
    page = int(ll)
    page -= 1
    currentpage = page
    if page > 1:
        updatetreeview(treeview, page)
        pagelabel.config(text=f"Page {currentpage}")
    else:
        if page == 1:
            currentpage = 1
            updatetreeview(treeview, 1)
            pagelabel.config(text=f"Page {currentpage}")


def nextpage(treeview, page_label):
    """

    :param treeview:
    :param page_label:
    :return:
    """
    ll = pagelabel.cget("text").replace('Page ', '')  # Page  .replace('Page','')  Page 1
    print('ll', ll)
    # print(str(tatalpage))
    page = int(ll) + 1
    # currentpage=currentpage+ 1
    print('next:', pagelabel.cget("text"))
    currentpage = page
    print('next:', page)
    if currentpage <= tatalpage:
        updatetreeview(treeview, currentpage)
        pagelabel.config(text=f"Page {currentpage}")


# 并创建主窗日
root = tk.Tk()
root.title="分頁"
treeview = ttk.Treeview(root, columns=("ID","First Name", "Last Name", "Email"))
treeview['show'] = 'headings'
treeview.heading("ID", text="ID")
treeview.heading("First Name", text="First Name")
treeview.heading("Last Name", text="Last Name")
treeview.heading("Email", text="Email")
treeview.pack()
prevbtn = tk.Button(root, text="Prev", command=lambda: prevpage(treeview, pagelabel))
prevbtn.pack(side="left")
nextbtn = tk.Button(root, text="Next", command=lambda: nextpage(treeview, pagelabel))
nextbtn.pack(side="left")
pagelabel = tk.Label(root, text="Page 1")
pagelabel.pack(side="left")

totallabel = tk.Label(root, text="/5")
totallabel.pack(side="left")
# 初始化Treeview和分页数据
currentpage = 1
updatetreeview(treeview, currentpage)
# 运行主循环
root.mainloop()

  

 

posted @ 2024-12-02 20:03  ®Geovin Du Dream Park™  阅读(33)  评论(0)    收藏  举报