当前位置:   article > 正文

Python Treeview标题栏排序和控件之间记录调用的应用体会_python的treeview函数的文件之间的调用

python的treeview函数的文件之间的调用

Python Treeview标题栏排序和控件之间记录调用的应用体会

在Python调用数据库记录时,有时需要用到Treeview控件来组织管理记录。在Treeview数据排列和其他控件调用Treeview选择的记录并显示出来,本人试着编写代码如下,运行通过。
实现代码:
from tkinter import *
from tkinter.messagebox import *
import sqlite3
from tkinter import ttk
 
dbstr = "F://My Pthon_py//tkdatagui.db"
 
root = Tk()
root.geometry('800x600')
root.title('学生管理系统')
 
Label(root, text="学号:").grid(row=2,column=0)
Label(root, text="姓名:").grid(row=2,column=2)
Label(root, text="电话:").grid(row=3,column=0)
Label(root, text="地址:").grid(row=3,column=2)
 
sid = StringVar()
name = StringVar()
phone = StringVar()
address = StringVar()
revx = True
Entry(root, textvariable=sid).grid(row=2,column=1)
Entry(root, textvariable=name).grid(row=2,column=3)
Entry(root, textvariable=phone).grid(row=3,column=1)
Entry(root, textvariable=address).grid(row=3,column=3)
 
Label(root, text='学生信息管理', bg='white', fg='red', font=('宋体', 15)).grid(row=0,column=0,columnspan=4)
 
 
def showAllInfo():
    x = dataTreeview.get_children()
    for item in x:
        dataTreeview.delete(item)
    con = sqlite3.connect(dbstr)
    cur = con.cursor()
    cur.execute("select * from tkbasegui")
    lst = cur.fetchall()
    for item in lst:
        dataTreeview.insert("", 1, text="line1", values=item)
    cur.close()
    con.close()
 
 
def appendInfo():
    if sid.get() == "":
        showerror(title='提示', message='输入不能为空')
    elif name.get() == "":
        showerror(title='提示', message='输入不能为空')
    elif phone.get() == "":
        showerror(title='提示', message='输入不能为空')
    elif address.get() == "":
        showerror(title='提示', message='输入不能为空')
    else:
        x = dataTreeview.get_children()
        for item in x:
            dataTreeview.delete(item)
        list1 = []
        list1.append(sid.get())
        list1.append(name.get())
        list1.append(phone.get())
        list1.append(address.get())
        con = sqlite3.connect(dbstr)
        cur = con.cursor()
        cur.execute("insert into tkbasegui values(?,?,?,?)", tuple(list1))
        con.commit()
        cur.execute("select * from tkbasegui")
        lst = cur.fetchall()
        for item in lst:
            dataTreeview.insert("", 1, text="line1", values=item)
        cur.close()
        con.close()
 
 
def deleteInfo():
    con = sqlite3.connect(dbstr)
    cur = con.cursor()
    cur.execute("select * from tkbasegui")
    studentList = cur.fetchall()
    cur.close()
    con.close()
    print(studentList)
 
    num = sid.get()
    flag = 0
    if num.isnumeric() == False:
        showerror(title='提示', message='删除失败')
    for i in range(len(studentList)):
        for item in studentList[i]:
            if int(num) == item:
                flag = 1
                con = sqlite3.connect(dbstr)
                cur = con.cursor()
                cur.execute("delete from tkbasegui where id = ?", (int(num),))
                con.commit()
                cur.close()
                con.close()
                break
    if flag == 1:
        showinfo(title='提示', message='删除成功!')
    else:
        showerror(title='提示', message='删除失败')
 
    x = dataTreeview.get_children()
    for item in x:
        dataTreeview.delete(item)
 
    con = sqlite3.connect(dbstr)
    cur = con.cursor()
    cur.execute("select * from tkbasegui")
    lst = cur.fetchall()
    for item in lst:
        dataTreeview.insert("", 1, text="line1", values=item)
    cur.close()
    con.close()
 
def treeview_sort_column(event): #点击标题排序
    # items =dataTreeview.selection() #获取点击的行号 I00n
    item3 = dataTreeview.identify("column",event.x,event.y) #获取点击的列号 #n
    if event.y<22: #判断是否点击标题行
       global revx
       col = item3
       l = [(dataTreeview.set(k, col), k) for k in dataTreeview.get_children('')] #构建列表[行列确定的数据 ,k行号]
       l.sort(reverse=revx)
       revx = not revx #排序模式转变
       for index, (val, k) in enumerate(l): #给列表添加序号,
           dataTreeview.move(k, '', index) #把k原行号数据移到index新行号处
    else:
        items = dataTreeview.selection()  # 获取点击的行号 I00n
        lstx = dataTreeview.item(items, "values") #获取点击行的整行数据
        sid.set(lstx[0])  #赋值给每个输入框
        name.set(lstx[1])
        phone.set(lstx[2])
        address.set(lstx[3])
Button(root, text="显示所有信息", command=showAllInfo).place(relx=0.2, rely=0.2, width=100)
Button(root, text="追加信息", command=appendInfo).place(relx=0.4, rely=0.2, width=100)
Button(root, text="删除信息", command=deleteInfo).place(relx=0.6, rely=0.2, width=100)
 
 
dataTreeview = ttk.Treeview(root, show='headings', column=('sid', 'name', 'phone', 'address'))
dataTreeview.column('sid', width=150, anchor="center")
dataTreeview.column('name', width=150, anchor="center")
dataTreeview.column('phone', width=150, anchor="center")
dataTreeview.column('address', width=150, anchor="center")
 
dataTreeview.heading('sid', text='学号')
dataTreeview.heading('name', text='名字')
dataTreeview.heading('phone', text='电话')
dataTreeview.heading('address', text='地址')
dataTreeview.bind("<ButtonRelease-1>",treeview_sort_column)
dataTreeview.place(relx=0.02,rely=0.3, relwidth=0.97)
showAllInfo() #打开窗口即自动导入全部数据
root.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
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152

  • 1
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/993263
推荐阅读
相关标签
  

闽ICP备14008679号