python: Treeview Pagination

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# 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 @   ®Geovin Du Dream Park™  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-12-02 javascript: jquery-confirm.js
2013-12-02 sql: 生日三个月内有效
2009-12-02 SQL 根据汉字获取全拼
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示