完整版,部分细节未实现
import pymysql
from tkinter import *
db=pymysql.Connect(host='localhost',port=3306,user='root',passwd='4758stuv870399',db='stuadm',charset='utf8')
cursor=db.cursor()
def showTheTable(Listbox):
Listbox.delete(0,END)
data=[]
sql1 = "SELECT * FROM studentadm_information"#构造sql语句
cursor.execute(sql1) # 获取全部数据
all_data=cursor.fetchall()
for i in all_data:
i=list(i)#名字在2-4之间补齐空格
if len(i[0])==2:
i[0]=i[0]+" "
if len(i[0])==3:
i[0]=i[0]+" "
dd='{}{:<11d}{}'.format(i[0],i[1],i[2])
Listbox.insert(END,dd)
def updateTheTable():#更新,获取数据,执行sql 更新表单
var1=entry1.get()
var2=int(entry2.get())
var3=entry3.get()
sql = "INSERT INTO studentadm_information values\
('%s',%d,'%s')" % (var1,var2,var3)
print(sql)
try:
cursor.execute(sql) # 没有异常提交数据,
db.commit()
print("学生信息提交成功!")
except:
print("上传信息失败!")
db.rollback()
showTheTable(mylist)
def changeTheTable():#构造sql 删除
var1=entrya.get()
var2=int(entryb.get())
var3=entryc.get()
try:
sql3 = "UPDATE studentadm_information set student_name=\'%s\',student_phoneNumber=%s WHERE student_id=%d" % (var1, var3, var2)
print(sql3)
cursor.execute(sql3)
db.commit()
except:
db.rollback()
showTheTable(mylist)
def deleteTheTable():
var1=int(entryp.get())
sql = "DELETE FROM studentadm_information where student_id=%d" % (var1)
try:
cursor.execute(sql)
db.commit()
print("删除成功")
except:
db.rollback()
showTheTable(mylist)
def checkTheTable():
var1 = int(entryA.get())
sql1 = "SELECT * FROM studentadm_information where student_id=%d" % (var1);
try:
cursor.execute(sql1)
s = cursor.fetchone()
print(s)
if s == None:
print("该学生不存在!")
db.commit()
var2=s[0]
var3=s[2]
name.set(var2)
phone.set(var3)
except:
print("wrong")
db.rollback()
showTheTable(mylist)
window=Tk()
window.geometry("700x500")#主窗口
window.title("学生信息管理系统")
#创建一个图片管理类
photo =PhotoImage(file=r'C:\Users\联想\Desktop\python\schooltask\images\1.gif')#file:t图片路径
imgLabel = Label(window,image=photo)#把图片整合到标签类中
imgLabel.pack(side=RIGHT)#自动对齐
top=Frame(window,height=200,width=600,bg='green');#显示学生信息
scrollar = Scrollbar(top)
scrollar.pack(side=RIGHT, fill=Y)
mylist=Listbox(top,height=10,width=50,yscrollcommand=scrollar.set)#绑定下拉事件
entry1=Entry(window)#1,2,3是增加按钮
entry2=Entry(window)
entry3=Entry(window)
entrya=Entry(window)#abc是改动的
entryb=Entry(window)
entryc=Entry(window)
entryA=Entry(window)#ABC是查看的
name= StringVar()#两个变量函数,用于和查询的两个entry绑定
phone=StringVar()
entryB=Entry(window,textvariable=name,state="readonly")
entryC=Entry(window,textvariable=phone,state="readonly")
entryp=Entry(window)#p是用来指定删除对象的
entry1.place(x=50,y=300)
entry2.place(x=200,y=300)
entry3.place(x=350,y=300)
entrya.place(x=50,y=340)
entryb.place(x=200,y=340)
entryc.place(x=350,y=340)
entryA.place(x=50,y=450)
entryB.place(x=250,y=450)
entryC.place(x=450,y=450)
entryp.place(x=350,y=380)
bt1=Button(window,text="增加",command=updateTheTable)
bt1.place(x=520,y=290)
bt2=Button(window,text="改动",command=changeTheTable)
bt2.place(x=520,y=330)
bt3=Button(window,text="删除",command=deleteTheTable)
bt3.place(x=520,y=370)
bt4=Button(window,text="查看",command=checkTheTable)
bt4.place(x=200,y=450)
showTheTable(mylist)
scrollar.config(command=mylist.yview)
mylist.pack(side=LEFT)
top.pack()
window.mainloop()