!!! For practice gui

Operating environment

already installed python3.7,mysql( Build a good library )

main program :mg1.py
import pymysql import tkinter as tk from tkinter import messagebox import
subprocess subprocess.call('ks1.py',shell=True)# call windows Administrator rights start mysql
ku=pymysql.connect(host="localhost",user=" Login account ",password=" User password ",db=" Library name ")# Link to Library , Need to add login account , Account password , Library name used
cursor=ku.cursor(cursor=pymysql.cursors.DictCursor)# Accept data as a list of elements as dictionaries def
init():# Initialize student table db=[] p=input(" Please type any characters to reinitialize the data table , Otherwise, type enter !") if p:
cursor.execute("drop table if exists biao")# If the table exists , Delete it 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}) # take db increase to MySQL In the table of 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(" Add a student record ")
second.geometry('300x300') # main Frame frame =tk.Frame(second) frame.pack() #
Two pieces Frame, Attention son frame It's in the Lord frame Under the framework of 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=' Please input student number ').pack() add_sno=tk.Entry(frame_r, show = None)
add_sno.pack() tk.Label(frame_l,text=' grade scores of Chinese ').pack() add_yw=tk.Entry(frame_r,
show = None) add_yw.pack() tk.Label(frame_l,text=' Mathematics achievement ').pack()
add_sx=tk.Entry(frame_r, show = None) add_sx.pack() tk.Label(frame_l,text=' Physics achievement
').pack() add_wl=tk.Entry(frame_r, show = None) add_wl.pack()
tk.Label(frame_l,text=' English achievement ').pack() add_en=tk.Entry(frame_r, show = None)
add_en.pack() # Save the results entered into the database b1=tk.Button(second,text=' determine ', 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()# Event rollback
tk.messagebox.showinfo(title = 'Hi', message = e) return ku.commit()
tk.messagebox.showinfo(title = 'Hi', message =" Increase success ") # Think about how to jump out of the window of increasing success , Close window increase
def delete(): global del_sno third=tk.Toplevel() third.title(" Delete a student record ")
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=' Please input student number ').pack()
del_sno=tk.Entry(frame_r, show = None) del_sno.pack()
b1=tk.Button(third,text=' determine ', 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 A value is returned , When the command is executed successfully, the 1, Failed for 0 ku.commit() else : ku.rollback()
tk.messagebox.showinfo(title = 'Hi', message =" Deletion failed ") return
tk.messagebox.showinfo(title = 'Hi', message =" Deletion succeeded ") def change(): global
fouth global change_sno global change_yw global change_sx global change_wl
global change_en fouth =tk.Toplevel() fouth.title(" Modify a student record ")
fouth.geometry('300x300') # main Frame frame =tk.Frame(fouth) frame.pack() #
Two pieces Frame, Attention son frame It's in the Lord frame Under the framework of 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=' Please input student number ').pack() change_sno=tk.Entry(frame_r, show =
None) change_sno.pack() tk.Label(frame_l,text=' grade scores of Chinese ').pack()
change_yw=tk.Entry(frame_r, show = None) change_yw.pack()
tk.Label(frame_l,text=' Mathematics achievement ').pack() change_sx=tk.Entry(frame_r, show = None)
change_sx.pack() tk.Label(frame_l,text=' Physics achievement ').pack()
change_wl=tk.Entry(frame_r, show = None) change_wl.pack()
tk.Label(frame_l,text=' English achievement ').pack() change_en=tk.Entry(frame_r, show = None)
change_en.pack() # Save the results entered into the database b1=tk.Button(fouth,text=' determine ', 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
=" Modification succeeded ") def query(): global select_sno fifth=tk.Toplevel()
fifth.title(" Query student records ") fifth.geometry('400x300')
tk.Label(fifth,text=' Please enter your query statement ( The query table is :biao)').pack() select_sno=tk.Entry(fifth,
show = None) select_sno.pack() b1=tk.Button(fifth,text=' determine ', 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(" Query a student record ") 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=' Please input student number ').pack() inquire_sno=tk.Entry(frame_r, show =
None) inquire_sno.pack() b1=tk.Button(sixth,text=' determine ', 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 A value is returned , When the command is executed successfully, the 1, Failed for 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={" chinese ":"yw"," mathematics ":"sx"," Physics ":"wl"," English ":"en"} for i in subject:
tk.Label(frame_l,text=i+' The result is : ').pack() g=subject[i]
tk.Label(frame_r,text=db[0][g]).pack() ku.commit() else : ku.rollback()
tk.messagebox.showinfo(title = 'Hi', message =" Query failed ") return init() w=tk.Tk()
w.title(' Achievement management system ')# Main window title w.geometry('500x350')# Window size
tk.Label(text=' Function list ',bg='yellow').pack()# label
an=tk.Button(w,text=" Add records ",command=add,bg='yellow')
an.pack(padx=20,pady=10,fill = 'x')
an=tk.Button(w,text=" Delete record ",command=delete,bg='yellow')
an.pack(padx=20,pady=10,fill = 'x')
an=tk.Button(w,text=" Modification record ",command=change,bg='yellow')
an.pack(padx=20,pady=10,fill = 'x')
an=tk.Button(w,text=" Query a student record ",command=inquire,bg='yellow')
an.pack(padx=20,pady=10,fill = 'x')
an=tk.Button(w,text=" Free inquiry of student records ",command=query,bg='yellow')
an.pack(padx=20,pady=10,fill = 'x')
an=tk.Button(w,text=" sign out ",command=exit,bg='yellow') an.pack(padx=20,pady=10,fill
= 'x') w.mainloop()
configuration file

In order to realize automatic code opening MySQL service , These two files are configured , Code to be optimized ;

①ks.py
# Try the python Access to administrator rights in cmd import subprocess a =
subprocess.call('cmd.py',shell=True)
②cmd.py
#python obtain windows Administrator rights # It will pop up uac Interface 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(): #
Add the code to run here 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)
Running result demonstration :

1, Feature list view

2, Add records

Add pop-up prompt window when failed

Successful addition shows successful addition

3, Delete record

Error delete also report error

4, Modify a student record

5, Query a student record

5, Free query record

Or enter a query statement

Technology