Python+tkinter库实现简单图书管理系统
文章目錄
- 前言
- 一、MySQL是什么?
- 二、Tkinter庫(kù)
- 三、Code步驟
- 1.引入庫(kù)
- 2.使用tkinter搭建系統(tǒng)
- 3.定義函數(shù)(發(fā)送消息,操作mysql數(shù)據(jù)庫(kù))
- 總結(jié)
前言
使用mysql數(shù)據(jù)庫(kù)存儲(chǔ)數(shù)據(jù),使用python中tkinter庫(kù)建立一個(gè)簡(jiǎn)單的圖書管理系統(tǒng)
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、MySQL是什么?
MySQL 是一種關(guān)聯(lián)數(shù)據(jù)庫(kù)管理系統(tǒng),關(guān)聯(lián)數(shù)據(jù)庫(kù)將數(shù)據(jù)保存在不同的表中,而不是將所有數(shù)據(jù)放在一個(gè)大倉(cāng)庫(kù)內(nèi),增加了速度并提高了靈活性。
- MySQL的使用教程
https://www.runoob.com/mysql/mysql-tutorial.html
二、Tkinter庫(kù)
Tkinter 是 Python 的標(biāo)準(zhǔn) GUI 庫(kù)。Python 使用 Tkinter 可以快速的創(chuàng)建 GUI 應(yīng)用程序。
由于 Tkinter 是內(nèi)置到 python 的安裝包中、只要安裝好 Python 之后就能 import Tkinter 庫(kù)、而且 IDLE 也是用 Tkinter 編寫而成、對(duì)于小白而言,搭建一個(gè)簡(jiǎn)單的圖形界面 Tkinter 還是一個(gè)不錯(cuò)的選擇。
- Tkinterl的使用教程
https://www.runoob.com/python/python-gui-tkinter.html
三、Code步驟
1.引入庫(kù)
import tkinter as tk import tkinter.ttk as ttk from pymysql import connect from tkinter.messagebox import showinfo from tkinter.constants import END, N, SINGLE2.使用tkinter搭建系統(tǒng)
代碼如下(示例):
#主窗口 root=tk.Tk() root.title("YU_book_manage_system") root.geometry("750x500+100+100")#菜單 menubar=tk.Menu(root) filemenu=tk.Menu(menubar,tearoff=0) menubar.add_cascade(label="菜單",menu=filemenu) filemenu.add_command(label="打開") filemenu.add_command(label="保存") filemenu.add_command(label="退出",command=root.destroy) root.config(menu=menubar)#面板&框架 pw=tk.PanedWindow(root,orient="vertical",sashrelief="sunken") pw.pack(fill="both",expand=1)pw1=tk.PanedWindow(pw,orient="horizontal",sashrelief="sunken") pw2=tk.PanedWindow(pw,orient="horizontal",sashrelief="sunken") top_frame,left_frame,mid_frame,right_frame=ttk.Frame(pw1,height=125,relief="flat"),\ttk.Frame(pw2,relief="sunken"),\ttk.Frame(pw2,relief="raised"),\ttk.Frame(pw2,relief="ridge")pw.add(pw1),pw.add(pw2),pw1.add(top_frame),pw2.add(left_frame),pw2.add(mid_frame),pw2.add(right_frame)labelframe=tk.LabelFrame(left_frame,text="圖書信息",font=("迷你簡(jiǎn)魏碑",16),labelanchor=N) ttk.Label(labelframe,text=("編號(hào)","圖書名稱","圖書作者","出版日期","圖書數(shù)量"),font=("華文行楷",11),anchor="n").pack()#列表框&滾動(dòng)條 scrollbar=ttk.Scrollbar(labelframe) listbox=tk.Listbox(labelframe,width=60,font=("黑體",11),height=20,yscrollcommand=scrollbar,selectmode=SINGLE) listbox.select_set(0) #鏈接滾動(dòng)條&列表框 scrollbar.config(command=listbox.yview)labelframe.pack(expand=0),scrollbar.pack(side="right",fill="y",pady=2),listbox.pack(side="left",pady=2)#初始listbox show_book()#填充文本&輸入框 ttk.Label(top_frame,text="圖書管理系統(tǒng)",font=("方正字跡-呂建德行楷繁體",25)).pack(pady=30) ttk.Label(mid_frame,text="圖書編號(hào)",font=("黑體",10)).place(x=0,y=55) ttk.Label(mid_frame,text="圖書名稱",font=("黑體",10)).place(x=0,y=120) ttk.Label(mid_frame,text="圖書作者",font=("黑體",10)).place(x=0,y=185) ttk.Label(mid_frame,text="出版日期",font=("黑體",10)).place(x=0,y=250) ttk.Label(mid_frame,text="圖書數(shù)量",font=("黑體",10)).place(x=0,y=315)#entry變量 v1=tk.StringVar() v2=tk.StringVar() v3=tk.StringVar() v4=tk.StringVar() v5=tk.StringVar()e1=ttk.Entry(mid_frame,width=16,textvariable=v1) e2=ttk.Entry(mid_frame,width=16,textvariable=v2) e3=ttk.Entry(mid_frame,width=16,textvariable=v3) e4=ttk.Entry(mid_frame,width=16,textvariable=v4) e5=ttk.Entry(mid_frame,width=16,textvariable=v5)#單獨(dú)放置(由于get方法) e1.pack(padx=5,pady=80) e2.place(x=5,y=145) e3.place(x=5,y=205) e4.place(x=5,y=270) e5.place(x=5,y=335)#消息發(fā)送 button1=ttk.Button(right_frame,text="查詢圖書",command=find_book).pack(pady=64) button2=ttk.Button(right_frame,text="增加圖書",command=add_book).pack() button3=ttk.Button(right_frame,text="修改圖書",command=mod_book).pack(pady=64) button4=ttk.Button(right_frame,text="刪除圖書",command=del_book).pack() button5=ttk.Button(mid_frame,text="寫入",command=get_data).place(x=20,y=15)root.mainloop()圖書管理界面截圖
3.定義函數(shù)(發(fā)送消息,操作mysql數(shù)據(jù)庫(kù))
函數(shù)建議放在前面,代碼如下(示例):
#添加圖書 def add_book():try:conn=connect(host="localhost",port=3306,database="book_manage",user="root",password="yusql",charset="utf8")cur=conn.cursor()cur.execute("select *from books")data=cur.fetchall()param2=e2.get()param3=e3.get()param4=e4.get()param5=int(e5.get())for item in data:if item[1]==param2 and item[2]==param3:param5+=1cur.execute("update books set bnumber=%s where btitle=%s and bauthor=%s",(param5,param2,param3))conn.commit()show_book()showinfo("窗口","圖書存在,數(shù)量加一")return()cur.execute("insert into books(btitle,bauthor,byu_date,bnumber)values(%s,%s,%s,%s)",(param2,param3,param4,param5))conn.commit()show_book()showinfo("窗口","添加成功")except:showinfo("窗口","輸入格式錯(cuò)誤")#刪除圖書 def del_book():conn=connect(host="localhost",port=3306,database="book_manage",user="root",password="yusql",charset="utf8")cur=conn.cursor()param1=int(e1.get())param2=e2.get()param3=e3.get()param4=e4.get()param5=int(e5.get())if param5>1:param5-=1cur.execute("update books set id=%s,btitle=%s,bauthor=%s,byu_date=%s,bnumber=%s where btitle=%s and bauthor=%s",(param1,param2,param3,param4,param5,param2,param3))conn.commit()show_book()showinfo("窗口","圖書存在,數(shù)量減一")return() else:cur.execute("delete from books where id=%s and btitle=%s",(param1,param2))conn.commit()cur.execute("alter table books drop id")cur.execute("alter table books add id int(12) primary key not null auto_increment first")show_book()showinfo("窗口","刪除成功")#更新圖書 def show_book():conn=connect(host="localhost",port=3306,database="book_manage",user="root",password="yusql",charset="utf8")cur=conn.cursor()cur.execute("select *from books")data=cur.fetchall()listbox.delete(0,END)for item in data:listbox.insert("end",item) cur.close()conn.close()#修改圖書 def mod_book():try:conn=connect(host="localhost",port=3306,database="book_manage",user="root",password="yusql",charset="utf8")cur=conn.cursor()param1=int(e1.get())param2=e2.get()param3=e3.get()param4=e4.get()param5=int(e5.get())cur.execute("update books set id=%s,btitle=%s,bauthor=%s,byu_date=%s,bnumber=%s where id=%s",(param1,param2,param3,param4,param5,param1))conn.commit()show_book()showinfo("窗口","修改成功")except:showinfo("窗口","輸入格式錯(cuò)誤")#查詢圖書 def find_book():try:conn=connect(host="localhost",port=3306,database="book_manage",user="root",password="yusql",charset="utf8")cur=conn.cursor()param=e2.get()cur.execute("select *from books")data=cur.fetchall()for item in data:if item[1]==param:showinfo("窗口",item)cur.close()conn.close()except:showinfo("窗口","無該圖書")#得到選中列表框數(shù)據(jù) def get_data():value=listbox.get(listbox.curselection())v1.set(value[0])v2.set(value[1])v3.set(value[2])v4.set(value[3])v5.set(value[4])總結(jié)
本文僅僅簡(jiǎn)單使用了tkinter搭建了一個(gè)簡(jiǎn)單的圖書管理系統(tǒng),你也可以搭建其他的信息管理系統(tǒng)或者對(duì)代碼進(jìn)行改進(jìn),添加更多的功能。
總結(jié)
以上是生活随笔為你收集整理的Python+tkinter库实现简单图书管理系统的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于mingw32-make的错误
- 下一篇: 一分钱不花,教你白piao一套自己的云笔