終極版終于有時(shí)間給大家分享了!!!。
我們先看一下效果圖。
1:登錄界面:
2:查詢數(shù)據(jù)庫(kù)所有的內(nèi)容!
3:鏈接數(shù)據(jù)庫(kù):
4:最終的打包!
話不多說(shuō)直接上代碼!!!!
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('登錄賬號(hào)!') def create(): root =Toplevel() root.geometry('700x800') root.title('學(xué)生管理系統(tǒng)') Label(root, text="學(xué)號(hào):").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(學(xué)號(hào),姓名,性別,電話) 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('提示', '請(qǐng)輸入學(xué)號(hào)!') 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="查詢個(gè)人信息", 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 = ('學(xué)號(hào)', '姓名', '性別', '電話') treeview = ttk.Treeview(root, show='headings', columns=columns) treeview.column('學(xué)號(hào)', width=150, anchor='center') treeview.column('姓名', width=150, anchor='center') treeview.column('性別', width=150, anchor='center') treeview.column('電話', width=150, anchor='center') treeview.heading('學(xué)號(hào)', text='學(xué)號(hào)') treeview.heading('姓名', text='姓名') treeview.heading('性別', text='性別') treeview.heading('電話', text='電話') treeview.place(rely=0.3, relwidth=0.97) Label(window,text = '賬號(hào):').place(relx =0, rely = 0.05,relwidth = 0.3) Label(window,text = '密碼:').place(relx = 0, rely = 0.15,relwidth =0.3) #鼠標(biāo)定位 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 #登陸函數(shù) def dl(): if zh.get() == '20' and mm.get() == '' : # showinfo('提示!','登錄成功!') # window.quit() return create() else: showerror('錯(cuò)誤!','賬號(hào)或密碼錯(cuò)誤!') Button(window,text = '登錄', command =dl).place(relx = 0.2 ,rely = 0.3, relwidth = 0.5) window.mainloop()
還有最后一步――代碼打包!!!
我們可以用庫(kù)――pyinstaller
下載方法:打開cmd 輸入 pip install pyinstaller
然后在Terminal里輸入 pyinstaller -D -w xxxx.py
然后就成功啦!!!!
最后總結(jié)一下:
這個(gè)gui界面其實(shí)還可以寫成學(xué)生登錄和老師登錄,再來(lái)一個(gè)注冊(cè)界面,都是用tk去實(shí)現(xiàn),然后老師和學(xué)生登陸后的功能不同,這個(gè)是一個(gè)想法,我沒有時(shí)間去寫了,有的小伙伴有時(shí)間可以去思考思考哦!!也歡迎和我討論,隨時(shí)歡迎!!!
最后點(diǎn)點(diǎn)贊吧,我快你沒動(dòng)力分享了!!!
到此這篇關(guān)于Python實(shí)戰(zhàn)項(xiàng)目之MySQL tkinter pyinstaller實(shí)現(xiàn)學(xué)生管理系統(tǒng)的文章就介紹到這了,更多相關(guān)Python 學(xué)生管理系統(tǒng)內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/m0_54355125/article/details/120070511