python: Treeview Control binding data using tkinter and ttkbootstrap GUI

 

pip install matplotlib
pip install numpy
pip install statsmodels

 

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
"""
StudentUI.py
读文件类
date 2023-06-24
edit: Geovin Du,geovindu, 涂聚文
ide:  PyCharm 2023.1 python 11
 
"""
 
import datetime
import sys
import os
from tkinter import ttk
from tkinter import *
from tkinter.ttk import *
from ttkbootstrap import Style  # pip install ttkbootstrap
import random
import Model.StudentListInfo
 
class StudentUi(object):
 
    global tree
    def __del__(self):
        self.name="geovindu"
 
    def delete():
        global tree
        tree.delete(tree.selection())
 
    def main():
 
        geovindu = list()
        geovindu.append(Model.StudentListInfo.StudentList(1, '王二', '002', datetime.datetime(2008, 2, 14, 0, 0)))
        geovindu.append(Model.StudentListInfo.StudentList(2, '涂年生', '003', datetime.datetime(2008, 2, 14, 0, 0)))
        geovindu.append(Model.StudentListInfo.StudentList(3, '涂聚文', '004', datetime.datetime(2008, 2, 14, 0, 0)))
        geovindu.append(Model.StudentListInfo.StudentList(4, '赵三', '001', datetime.datetime(2008, 2, 14, 0, 0)))
        geovindu.append(Model.StudentListInfo.StudentList(5, '李四', '005', datetime.datetime(2008, 2, 14, 0, 0)))
 
        style=Style(theme='darkly') #定义窗口样式
        window=style.master
        window.title("学生管理")
        # win = Tk()
        screenWidth = window.winfo_screenwidth()
        screenHeight = window.winfo_screenheight()
        width=100
        height=600
        x=int((screenWidth-width)/2)
        y=int((screenHeight-height)/2)
        window.geometry('{}x{}+{}+{}'.format(width,height,x,y))
        #Treeview 控件
        tree=ttk.Treeview(master=window,style='success.Treeview',height=25,show='headings')
        tree.pack()
        #定义列
        tree['columns']=("StudentId","StudentName","StudentNO","StudentBirthday")
        #设置列属性,列不显示
        tree.column("StudentId",width=150,minwidth=100,anchor=S)
        tree.column("StudentName", width=150, minwidth=100, anchor=S)
        tree.column("StudentNO", width=150, minwidth=100, anchor=S)
        tree.column("StudentBirthday", width=150, minwidth=100, anchor=S)
        #设置表头
        tree.heading("StudentId",text="序号")
        tree.heading("StudentName", text="姓名")
        tree.heading("StudentNO", text="学号")
        tree.heading("StudentBirthday", text="出生日期")
        #treeView控件绑定数据
        i=1
        for Model.StudentListInfo.StudentList in geovindu:
            tree.insert("",i,text="2",values=(Model.StudentListInfo.StudentList.getStudentId(),Model.StudentListInfo.StudentList.getStudentName(),Model.StudentListInfo.StudentList.getStudentNO(),Model.StudentListInfo.StudentList.getStudentBirthday()))
            i+=1
        #删除按钮
        ttk.Button(window,text="del",style='success,TButton',command=StudentUi.delete).pack(side='left',padx=5,pady=10)
        window.mainloop()

  

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
"""
StudentUI.py
读文件类
date 2023-06-24
edit: Geovin Du,geovindu, 涂聚文
ide:  PyCharm 2023.1 python 11
 
"""
 
import datetime
import sys
import os
from tkinter import ttk
from tkinter import *
from tkinter.ttk import *
from ttkbootstrap import Style  # pip install ttkbootstrap
import random
import Model.StudentListInfo
import BLL.StudentListBLL
 
class StudentUi(object):
 
    global tree
    stubll = BLL.StudentListBLL.StudentBll()
 
    def __init__(self):
        self.name="geovindu"
 
    def __del__(self):
        print(f"{self.name}")
 
    def delete(cls):
        #global tree
        curItem = cls.tree.focus()
        val=cls.tree.item(curItem)['values'][0#id
        print(val)
        print(cls.tree.selection())
        cls.tree.delete(cls.tree.selection())
        cls.stubll.delSql(val)  #需要删除关联的数据才可以删除
 
        #cls.stubll.delSql()
 
    def treeview_sort_column(cls,tv, col, reverse):
        """
        Treeview、列名、排列方式
        :param tv: Treeview 控件
        :param col:  列名
        :param reverse:
        :return:
        """
        l = [(tv.set(k, col), k) for k in tv.get_children('')]
        # print(tv.get_children(''))
        l.sort(reverse=reverse)  # 排序方式
        # rearrange items in sorted positions
        for index, (val, k) in enumerate(l):  # 根据排序后索引移动
            tv.move(k, '', index)
            # print(k)
        tv.heading(col, command=lambda: cls.treeview_sort_column(
            tv, col, not reverse))  # 重写标题,使之成为再点倒序的标题
 
    def main(cls):
        """
        窗体绑定数据
        :return:
        """
        #stubll = BLL.StudentListBLL.StudentBll()
        geovindu =cls.stubll.selectSqlOrder("StudentNO asc") # list()
 
        style=Style(theme='darkly') #定义窗口样式
        window=style.master
        window.title("学生管理")
        # win = Tk()
        screenWidth = window.winfo_screenwidth()
        screenHeight = window.winfo_screenheight()
        width=800
        height=600
        x=int((screenWidth-width)/2)
        y=int((screenHeight-height)/2)
        window.geometry('{}x{}+{}+{}'.format(width,height,x,y))
        #Treeview 控件
        cls.tree=ttk.Treeview(master=window,style='success.Treeview',height=25,show='headings')
        cls.tree.pack()
        #定义列
        cls.tree['columns']=("StudentId","StudentName","StudentNO","StudentBirthday","Age")
        #设置列属性,列不显示
        cls.tree.column("StudentId",width=150,minwidth=100,anchor=S)
        cls.tree.column("StudentName", width=150, minwidth=100, anchor=S)
        cls.tree.column("StudentNO", width=150, minwidth=100, anchor=S)
        cls.tree.column("StudentBirthday", width=150, minwidth=100, anchor=S)
        cls.tree.column("Age", width=150, minwidth=100, anchor=S)
        #设置表头
        cls.tree.heading("StudentId",text="序号")
        cls.tree.heading("StudentName", text="姓名")
        cls.tree.heading("StudentNO", text="学号")
        cls.tree.heading("StudentBirthday", text="出生日期")
        cls.tree.heading("Age", text="年龄")
 
        #删除按钮
        #ttk.Button(window,text="删除",style='success,TButton',command=cls.delete).pack(side='left',padx=5,pady=10)
 
        #Button(window, text='添加', bg='yellow', width=20).pack(side=LEFT)
        #Button(window, text='删除', bg='pink', width=20, command=cls.delete).pack(side=LEFT)
 
        #treeView控件绑定数据
        i=1
        for Model.StudentListInfo.StudentList in geovindu:
            cls.tree.insert("",i,text="2",values=(Model.StudentListInfo.StudentList.getStudentId(),Model.StudentListInfo.StudentList.getStudentName(),Model.StudentListInfo.StudentList.getStudentNO(),Model.StudentListInfo.StudentList.getStudentBirthday(),Model.StudentListInfo.StudentList.getAge()))
            i+=1
 
 
        columnnames=("序号","姓名","学号","出生日期","年龄")
        print(cls.tree["columns"])
        duindex=0
        # 控件treeview点击标题排序
        for col in cls.tree["columns"]:  # 给所有标题加cls.tree["columns"]
            cls.tree.heading(
                col,
                text=columnnames[duindex],  #中文名称
                command=lambda _col=col: cls.treeview_sort_column(
                    cls.tree, _col, False
                ),
            )
            duindex+=1
 
        vbar = ttk.Scrollbar(window, orient="vertical", command=cls.tree.yview)
        cls.tree.configure(yscrollcommand=vbar.set)
        cls.tree.grid(row=1, column=0, sticky="news")
        vbar.grid(row=1, column=1, sticky="ns")
 
 
        window.mainloop()

  

输出:

 

posted @   ®Geovin Du Dream Park™  阅读(70)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2020-06-24 csharp: Cyotek.GhostScript.Pdf Conversion pdf convert image
2020-06-24 csharp: using Acrobat.dll pdf convert images in winform
2016-06-24 csharp:VerifyCode in winform or webform
2015-06-24 csharp:获取 DNS、网关、子网掩码、IP
2015-06-24 csharp: using wininet.dll
2013-06-24 csharp:汉字转带拼音声调
< 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
点击右上角即可分享
微信分享提示