!!!用来练习gui

运行环境

已安装python3.7,mysql(建好库)

主程序:mg1.py
import pymysql import tkinter as tk from tkinter import messagebox import
subprocess subprocess.call('ks1.py',shell=True)#调用windows管理员权限启动mysql
ku=pymysql.connect(host="localhost",user="登陆账户",password="用户密码",db="库名")#链接到库,需添加登陆账户、账户密码、所使用库名
cursor=ku.cursor(cursor=pymysql.cursors.DictCursor)#接受数据为元素为字典的列表 def
init():#初始化学生表 db=[] p=input("重新初始化数据表请键入任意字符,否则请键入回车!") if p:
cursor.execute("drop table if exists biao")#如果表存在,删除它 sql="create table
biao(sno varchar(20) not null,yw smallint,sx smallint,wl smallint,en
smallint,primary key(sno));" cursor.execute(sql)
db.append({"sno":"12001","yw":44,"sx":57,"wl":57,"en":68})
db.append({"sno":"12002","yw":54,"sx":67,"wl":57,"en":78})
db.append({"sno":"12003","yw":74,"sx":57,"wl":57,"en":68})
db.append({"sno":"12004","yw":84,"sx":97,"wl":57,"en":98})
db.append({"sno":"12005","yw":34,"sx":37,"wl":57,"en":38}) #将db增加到MySQL的表中 for
i in db: a1="insert into biao(sno,yw,sx,wl,en) values (" a2=str(i['sno'])
a3=str(i['yw']) a4=str(i['sx']) a5=str(i['wl']) a6=str(i['en'])
sql1=a1+a2+","+a3+","+a4+","+a5+","+a6+")" cursor.execute(sql1) ku.commit() def
add(): global second global add_sno global add_yw global add_sx global add_wl
global add_en second =tk.Toplevel() second.title("增加一条学生记录")
second.geometry('300x300') # 主Frame frame =tk.Frame(second) frame.pack() #
两个子Frame,注意子frame是在主frame的框架下 frame_l =tk.Frame(frame) frame_r =tk.Frame(frame)
frame_l.pack(side = 'left') frame_r.pack(side = 'right')
tk.Label(frame_l,text='请输入学号 ').pack() add_sno=tk.Entry(frame_r, show = None)
add_sno.pack() tk.Label(frame_l,text='语文成绩 ').pack() add_yw=tk.Entry(frame_r,
show = None) add_yw.pack() tk.Label(frame_l,text='数学成绩 ').pack()
add_sx=tk.Entry(frame_r, show = None) add_sx.pack() tk.Label(frame_l,text='物理成绩
').pack() add_wl=tk.Entry(frame_r, show = None) add_wl.pack()
tk.Label(frame_l,text='英语成绩 ').pack() add_en=tk.Entry(frame_r, show = None)
add_en.pack() #将录入的成绩保存进数据库中 b1=tk.Button(second,text='确定', width=15, height=2,
command=insert_1) b1.pack() second.mainloop() def insert_1(): s0= add_sno.get()
s1= add_yw.get() s2= add_sx.get() s3= add_wl.get() s4= add_en.get() a1="insert
into biao(sno,yw,sx,wl,en) values (" sql1=a1+s0+","+s1+","+s2+","+s3+","+s4+")"
try: cursor.execute(sql1) ku.commit() except Exception as e: ku.rollback()#事件回滚
tk.messagebox.showinfo(title = 'Hi', message = e) return ku.commit()
tk.messagebox.showinfo(title = 'Hi', message ="增加成功") #思考如何在跳出增加成功弹窗后,关闭增加窗口
def delete(): global del_sno third=tk.Toplevel() third.title("删除一条学生记录")
third.geometry('300x300') frame =tk.Frame(third) frame.pack() frame_l
=tk.Frame(frame) frame_r =tk.Frame(frame) frame_l.pack(side = 'left')
frame_r.pack(side = 'right') tk.Label(frame_l,text='请输入学号 ').pack()
del_sno=tk.Entry(frame_r, show = None) del_sno.pack()
b1=tk.Button(third,text='确定', width=15, height=2, command=delete_1) b1.pack()
third.mainloop() def delete_1(): id1=del_sno.get() sql3="delete from biao where
sno="+id1 try: i=cursor.execute(sql3) except Exception as e:
tk.messagebox.showinfo(title = 'Hi', message = e) if i!=0:
#mysql会返回一个值,当命令执行成功为1,失败为0 ku.commit() else : ku.rollback()
tk.messagebox.showinfo(title = 'Hi', message ="删除失败") return
tk.messagebox.showinfo(title = 'Hi', message ="删除成功") def change(): global
fouth global change_sno global change_yw global change_sx global change_wl
global change_en fouth =tk.Toplevel() fouth.title("修改一条学生记录")
fouth.geometry('300x300') # 主Frame frame =tk.Frame(fouth) frame.pack() #
两个子Frame,注意子frame是在主frame的框架下 frame_l =tk.Frame(frame) frame_r =tk.Frame(frame)
frame_l.pack(side = 'left') frame_r.pack(side = 'right')
tk.Label(frame_l,text='请输入学号 ').pack() change_sno=tk.Entry(frame_r, show =
None) change_sno.pack() tk.Label(frame_l,text='语文成绩 ').pack()
change_yw=tk.Entry(frame_r, show = None) change_yw.pack()
tk.Label(frame_l,text='数学成绩 ').pack() change_sx=tk.Entry(frame_r, show = None)
change_sx.pack() tk.Label(frame_l,text='物理成绩 ').pack()
change_wl=tk.Entry(frame_r, show = None) change_wl.pack()
tk.Label(frame_l,text='英语成绩 ').pack() change_en=tk.Entry(frame_r, show = None)
change_en.pack() #将录入的成绩保存进数据库中 b1=tk.Button(fouth,text='确定', width=15,
height=2, command=update_1) b1.pack() fouth.mainloop() def update_1(): s0=
change_sno.get() s1= change_yw.get() s2= change_sx.get() s3= change_wl.get()
s4= change_en.get() a1="update biao set yw=" +s1+ ",sx=" +s2+ ",wl=" +s3+
",en=" +s4+" where sno=" +s0 sql1=a1 try: cursor.execute(sql1) ku.commit()
except Exception as e: ku.rollback() tk.messagebox.showinfo(title = 'Hi',
message = e) return ku.commit() tk.messagebox.showinfo(title = 'Hi', message
="修改成功") def query(): global select_sno fifth=tk.Toplevel()
fifth.title("查询学生记录") fifth.geometry('400x300')
tk.Label(fifth,text='请输入您的查询语句(查询表为:biao)').pack() select_sno=tk.Entry(fifth,
show = None) select_sno.pack() b1=tk.Button(fifth,text='确定', width=15,
height=2, command=select_1) b1.pack() b1=tk.Button(fifth,text=' ', width=15,
height=2, command=select_2('select * from biao')) b1.pack() fifth.mainloop()
def select_1(): fifth_1=tk.Toplevel() fifth_1.geometry('400x100') t =
tk.Text(fifth_1, height=8) t.pack() order=select_sno.get()
cursor.execute(order) db=cursor.fetchall() for i in db: t.insert('end', i)
t.insert('end','\n') def select_2(order): fifth_2=tk.Toplevel()
fifth_2.geometry('400x100') t = tk.Text(fifth_2, height=8) t.pack()
cursor.execute(order) db=cursor.fetchall() for i in db: t.insert('end', i)
t.insert('end','\n') def inquire(): global inquire_sno sixth=tk.Toplevel()
sixth.title("查询一条学生记录") sixth.geometry('300x150') frame =tk.Frame(sixth)
frame.pack() frame_l =tk.Frame(frame) frame_r =tk.Frame(frame)
frame_l.pack(side = 'left') frame_r.pack(side = 'right')
tk.Label(frame_l,text='请输入学号 ').pack() inquire_sno=tk.Entry(frame_r, show =
None) inquire_sno.pack() b1=tk.Button(sixth,text='确定', width=15, height=2,
command=inquire_1) b1.pack() sixth.mainloop() def inquire_1():
id2=inquire_sno.get() sql_order="select * from biao where sno="+id2 try:
j=cursor.execute(sql_order) #mysql会返回一个值,当命令执行成功为1,失败为0 except Exception as e:
tk.messagebox.showinfo(title = 'Hi', message = e) if j!=0:
sixth_1=tk.Toplevel() sixth_1.geometry('400x100') frame =tk.Frame(sixth_1)
frame.pack() frame_l =tk.Frame(frame) frame_r =tk.Frame(frame)
frame_l.pack(side = 'left') frame_r.pack(side = 'right') db=cursor.fetchall()
subject={"语文":"yw","数学":"sx","物理":"wl","英语":"en"} for i in subject:
tk.Label(frame_l,text=i+'成绩为: ').pack() g=subject[i]
tk.Label(frame_r,text=db[0][g]).pack() ku.commit() else : ku.rollback()
tk.messagebox.showinfo(title = 'Hi', message ="查询失败") return init() w=tk.Tk()
w.title('成绩管理系统')#主窗口标题 w.geometry('500x350')#窗口尺寸
tk.Label(text='功能列表',bg='yellow').pack()#标签
an=tk.Button(w,text="增加记录",command=add,bg='yellow')
an.pack(padx=20,pady=10,fill = 'x')
an=tk.Button(w,text="删除记录",command=delete,bg='yellow')
an.pack(padx=20,pady=10,fill = 'x')
an=tk.Button(w,text="修改记录",command=change,bg='yellow')
an.pack(padx=20,pady=10,fill = 'x')
an=tk.Button(w,text="查询一个学生记录",command=inquire,bg='yellow')
an.pack(padx=20,pady=10,fill = 'x')
an=tk.Button(w,text="自由查询学生记录",command=query,bg='yellow')
an.pack(padx=20,pady=10,fill = 'x')
an=tk.Button(w,text="退出",command=exit,bg='yellow') an.pack(padx=20,pady=10,fill
= 'x') w.mainloop()
配置文件

为实现代码自动开启MySQL服务,配置了这两个文件、代码有待优化;

①ks.py
#尝试在python中获取管理员权限的cmd import subprocess a =
subprocess.call('cmd.py',shell=True)
②cmd.py
#python获取windows管理员权限 #会弹出uac界面 from __future__ import print_function import
ctypes, sys import subprocess def is_admin(): try: return
ctypes.windll.shell32.IsUserAnAdmin() except: return False if is_admin(): #
将要运行的代码加到这里 a = subprocess.call('net start mysql',shell=True) print(a) else: if
sys.version_info[0] == 3: ctypes.windll.shell32.ShellExecuteW(None, "runas",
sys.executable, __file__, None, 1) else:#in python2.x
ctypes.windll.shell32.ShellExecuteW(None, u"runas", unicode(sys.executable),
unicode(__file__), None, 1)
运行结果演示:

1、功能列表视图

2、增加记录

增加失败时弹出提示弹窗

增加成功显示增加成功

3、删除记录

错误删除亦报错

4、修改一条学生记录

5、查询一条学生记录

5、自由查询记录

或者输入查询语句

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信