赞
踩
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()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。