Python實戰項目之MySQL tkinter pyinstaller實現學生管理系統

終極版終於有時間給大傢分享瞭!!!。

我們先看一下效果圖。

1:登錄界面:

2:查詢數據庫所有的內容!

3:鏈接數據庫:

4:最終的打包!

話不多說直接上代碼!!!!

from tkinter import *
import pymysql
from tkinter.messagebox import *
from tkinter import ttk
 
def get_connect():
    conn = pymysql.connect(host='localhost', user="root", passwd='GAO147258369',database='stu')
    return conn
 
window = Tk()
window.geometry('500x300')
window.title('登錄賬號!')
 
def create():
    root =Toplevel()
    root.geometry('700x800')
    root.title('學生管理系統')
 
    Label(root, text="學號:").place(relx=0, rely=0.05, relwidth=0.1)
    Label(root, text="姓名:").place(relx=0.5, rely=0.05, relwidth=0.1)
    Label(root, text="性別:").place(relx=0, rely=0.1, relwidth=0.1)
    Label(root, text="電話:").place(relx=0.5, rely=0.1, relwidth=0.1)
 
    sid1 = StringVar()
    name1 = StringVar()
    sex1 = StringVar()
    phone = StringVar()
    Entry(root, textvariable=sid1).place(relx=0.1, rely=0.05, relwidth=0.37, height=25)
    Entry(root, textvariable=name1).place(relx=0.6, rely=0.05, relwidth=0.37, height=25)
 
    Entry(root, textvariable=sex1).place(relx=0.1, rely=0.1, relwidth=0.37, height=25)
    Entry(root, textvariable=phone).place(relx=0.6, rely=0.1, relwidth=0.37, height=25)
 
    def add():
        list1 = []
        list1.append(sid1.get())
        list1.append(name1.get())
        list1.append(sex1.get())
        list1.append(phone.get())
        # print(list1)
        connection = get_connect()
        cur = connection.cursor(cursor=pymysql.cursors.DictCursor)
        sql = 'insert into student(學號,姓名,性別,電話) values("%s","%s","%s", "%s")'
        sid = int(list1[0])
        name = list1[1]
        sex = list1[2]
        iphone = int(list1[3])
        try:
            cur.execute(sql % (sid, name, sex, iphone))
            connection.commit()
        except Exception as e:
            connection.rollback()
            raise e
        finally:
            connection.close()
        showinfo('提示', '添加成功!')
 
    def search():
        showinfo('提示', '請輸入學號!')
        into = int(sid1.get())
        conn = pymysql.connect(host='localhost', user="root", passwd="", database='stu')
        cur = conn.cursor()
        sql = 'select * from student ;'
        cur.execute(sql)
        all = cur.fetchall()
        for i in range(len(all)):
            for j in all:
                if into == j[0]:
                    treeview.insert('', i, value=(j[0], j[1], j[2], j[3]))
                    break
            break
 
    def search_all():
        conn = pymysql.connect(host='localhost', user="root", passwd="", database='stu')
        cur = conn.cursor()
        sql = 'select * from student ;'
        cur.execute(sql)
        f = cur.fetchall()
        for i in range(len(f)):
            # for j in range(len(i)):
            treeview.insert('', i, value=(f[i][0], f[i][1], f[i][2], f[i][3]))
 
    Button(root, text="添加信息", command=add).place(relx=0.1, rely=0.2, width=100)
    Button(root, text="查詢個人信息", command=search).place(relx=0.3, rely=0.2, width=100)
    Button(root, text="查詢所有信息", command=search_all).place(relx=0.6, rely=0.2, width=100)
    Button(root, text="退出程序", command=root.quit).place(relx=0.8, rely=0.2, width=100)
 
    columns = ('學號', '姓名', '性別', '電話')
    treeview = ttk.Treeview(root, show='headings', columns=columns)
    treeview.column('學號', width=150, anchor='center')
    treeview.column('姓名', width=150, anchor='center')
    treeview.column('性別', width=150, anchor='center')
    treeview.column('電話', width=150, anchor='center')
 
    treeview.heading('學號', text='學號')
    treeview.heading('姓名', text='姓名')
    treeview.heading('性別', text='性別')
    treeview.heading('電話', text='電話')
 
    treeview.place(rely=0.3, relwidth=0.97)
Label(window,text = '賬號:').place(relx =0, rely = 0.05,relwidth = 0.3)
Label(window,text = '密碼:').place(relx = 0, rely = 0.15,relwidth =0.3)
 
#鼠標定位
zh = StringVar()
mm = StringVar()
#輸入框
Entry(window,textvariable =zh, show = None).place(relx =0.3,rely = 0.05,relwidth = 0.3)
Entry(window,textvariable =mm,show ='*').place(relx = 0.3,rely = 0.15 ,relwidth = 0.3)
 
#歡迎大傢找小寶交流哦QQ:2922035952
#登陸函數
def dl():
    if zh.get() == '20' and mm.get() == '' :
        # showinfo('提示!','登錄成功!')
        # window.quit()
        return create()
    else:
        showerror('錯誤!','賬號或密碼錯誤!')
 
Button(window,text = '登錄', command =dl).place(relx = 0.2 ,rely = 0.3, relwidth = 0.5)
 
window.mainloop()

還有最後一步——代碼打包!!!

我們可以用庫——pyinstaller

下載方法:打開cmd 輸入 pip install pyinstaller

然後在Terminal裡輸入 pyinstaller -D -w xxxx.py

然後就成功啦!!!!

最後總結一下:

這個gui界面其實還可以寫成學生登錄和老師登錄,再來一個註冊界面,都是用tk去實現,然後老師和學生登陸後的功能不同,這個是一個想法,我沒有時間去寫瞭,有的小夥伴有時間可以去思考思考哦!!也歡迎和我討論,隨時歡迎!!!

最後點點贊吧,我快你沒動力分享瞭!!!

到此這篇關於Python實戰項目之MySQL tkinter pyinstaller實現學生管理系統的文章就介紹到這瞭,更多相關Python 學生管理系統內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: