本文共 12957 字,大约阅读时间需要 43 分钟。
MySQL+Python仓库管理系统 窗口可视化管理系统
这是连接MySQL数据库的小系统,可以实现在可视界面窗口对数据库进行操作
pycharm代码如下
// An highlighted blockimport pymysql.cursorsfrom tkinter import ttkimport tkinter as tkimport tkinter.font as tkFontfrom tkinter import * # 图形界面库import tkinter.messagebox as messageboximport time# 连接数据库connect = pymysql.Connect( host='localhost', port=3306, user='root', passwd='dudu010112',# 这个是自己MySQL数据库密码 db='仓库管理系统2', # 自己创建的数据库名称 charset='utf8')# 主页面class StartPage: def __init__(self, parent_window): parent_window.destroy() # 销毁子界面 self.window = tk.Tk() self.window.title('仓库管理系统') self.window.geometry('700x600+70+50') def getTime(): timeStr = time.strftime('%H:%M:%S') Rtime.configure(text=timeStr) self.window.after(1000, getTime) Rtime = Label(self.window, text='') Rtime.pack(pady=25) getTime() label = Label(self.window, text="仓库管理系统", font=("楷体", 30)) label.pack(pady=10) # pady=100 界面的长度 # 按钮 Button(self.window, text="入库操作", font=tkFont.Font(size=16), command=lambda: xinjian(self.window), width=20, height=2, fg='white', bg='gray').place(x=100, y=300) Button(self.window, text="仓库查询", font=tkFont.Font(size=16), command=lambda: cangkucha(self.window), width=20, height=2, fg='white', bg='gray').place(x=400, y=300) Button(self.window, text="出库操作", font=tkFont.Font(size=16), command=lambda: chuku(self.window), width=20, height=2, fg='white', bg='gray').place(x=100, y=400) Button(self.window, text="退出系统", font=tkFont.Font(size=16), command=self.window.destroy, width=20, height=2, fg='white', bg='gray').place(x=400, y=400) self.window.mainloop()# 入库操作页面class xinjian: def __init__(self, parent_window): parent_window.destroy() # 销毁子界面 self.window = tk.Tk() self.window.title('入库操作') self.window.geometry('700x600+70+50') self.top_title = Label(self.window, text='入库操作', bg='SkyBlue', font=('楷体', 20), width=70, height=2) self.top_title.pack() self.var_id = StringVar() self.var_name = StringVar() self.var_gender = StringVar() self.var_age = StringVar() self.var_gid = StringVar() self.right_top_id_label = Label(text="入库单号:", font=('楷体', 15)).pack(pady=15) self.right_top_id_entry = Entry(textvariable=self.var_id, font=('楷体', 15)).pack() self.right_top_name_label = Label(text="货物编号", font=('楷体', 15)).pack(pady=15) self.right_top_name_entry = Entry(textvariable=self.var_name, font=('楷体', 15)).pack() self.right_top_gender_label = Label(text="入库量", font=('楷体', 15)).pack(pady=15) self.right_top_gender_entry = Entry(textvariable=self.var_gender, font=('楷体', 15)).pack() self.right_top_gender_label = Label(text="货物名称", font=('楷体', 15)).pack(pady=15) self.right_top_gender_entry = Entry(textvariable=self.var_age, font=('楷体', 15)).pack() self.right_top_gender_label = Label(text="供应商编号", font=('楷体', 15)).pack(pady=15) self.right_top_gender_entry = Entry(textvariable=self.var_gid, font=('楷体', 15)).pack() self.right_top_button1 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30) self.right_top_button2 = ttk.Button(text='返回', width=20, command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) # 捕捉右上角关闭点击 self.id = [] self.name = [] self.gender = [] self.age = [] self.gid = [] # 打开数据库连接 db = pymysql.connect("localhost", "root", "dudu010112", "仓库管理系统2") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "SELECT * FROM 入库" # SQL 查询语句 try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: self.id.append(row[0]) self.name.append(row[1]) self.gender.append(row[2]) self.age.append(row[3]) self.gid.append(row[4]) except: print("Error: unable to fetch data") messagebox.showinfo('警告!', '数据库连接失败!') db.close() # 关闭数据库连接 def back(self): StartPage(self.window) # 显示主窗口 销毁本窗口 def new_row(self): if self.var_id.get() != '' and self.var_name.get() != '' and\ self.var_gender.get() != '' and self.var_age.get() != '' and self.var_gid.get() != '': db = pymysql.connect("localhost", "root", "dudu010112", "仓库管理系统2") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "INSERT INTO 入库(入库单号,货物编号,入库量,货物名称,供应商编号) VALUES ('%s', '%s','%s','%s','%s')" % \ (self.var_id.get(), self.var_name.get(),self.var_gender.get(), self.var_age.get(),self.var_gid.get()) # SQL 插入语句 try: cursor.execute(sql) # 执行sql语句 db.commit() # 提交到数据库执行 messagebox.showinfo('提示!', '入库成功!') except: db.rollback() # 发生错误时回滚 messagebox.showinfo('警告!', '数据库连接失败!') db.close() # 关闭数据库连接 else: messagebox.showinfo('提示!', '请填写入库信息')# 仓库清单class cangkudan: def __init__(self, parent_window): parent_window.destroy() # 销毁子界面 self.window = tk.Tk() self.window.title('仓库清单') self.window.geometry('1200x600+70+50') db = pymysql.connect("localhost", "root", "dudu010112", "仓库管理系统2") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "SELECT * FROM 仓库" # SQL 语句 try: cursor.execute(sql) # 执行sql语句 results = cursor.fetchall() for row in results: self.name = '货物名称:' + row[0] self.id = '货物编号:' + row[1] self.num = '货物数量' + row[2] self.gid = '供应商编号' + row[3] db.commit() # 提交到数据库执行 Label(self.window, text=self.id + "\t" + self.name + "\t" + self.num+"\t"+ self.gid ,font=('楷体', 18)).pack(pady=5) except: db.rollback() # 发生错误时回滚 messagebox.showinfo('警告!', '数据库连接失败!') db.close() # 关闭数据库连接 self.right_top_button4 = ttk.Button(text='返回', width=20, command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) def back(self): cangkucha(self.window)# 仓库查询class cangkucha: def __init__(self, parent_window): parent_window.destroy() # 销毁子界面 self.window = tk.Tk() self.window.title('库存查询') self.window.geometry('700x600+70+50') self.student_id = StringVar() self.id = '货物编号:' + '' self.name = '货物名称:' + '' self.num = '货物数量:' + '' self.gid = '供应商编号:' + '' Button(self.window, text="库存清单", font=tkFont.Font(size=12), command=lambda: cangkudan(self.window), width=20, height=2, fg='white', bg='gray').place(x=20, y=70) self.right_top_name_label = Label(text="库存查询", font=('楷体', 15)).pack(pady=15) self.right_top_name_entry = Entry(textvariable=self.student_id, font=('楷体', 15)).pack(pady=30) self.right_top_button3 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30) self.right_top_button4 = ttk.Button(text='返回', width=20, command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) # 打开数据库连接 db = pymysql.connect("localhost", "root", "dudu010112", "仓库管理系统2") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "SELECT * FROM 仓库 WHERE 货物编号 = '%s'" % (self.student_id.get()) # SQL 查询语句 try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: self.id = '仓库编号:' + row[0] self.name = '货物名称:' + row[1] self.num = '货物数量:' + row[2] self.gid = '供应商编号:' + row[3] except: print("Error: unable to fetch data") db.close() # 关闭数据库连接 def back(self): StartPage(self.window) def new_row(self): if self.student_id.get() != '': db = pymysql.connect("localhost", "root", "dudu010112", "仓库管理系统2") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "SELECT * FROM 仓库 where 货物编号 = '%s'" % (self.student_id.get()) # SQL 插入语句 try: cursor.execute(sql) # 执行sql语句 results = cursor.fetchall() for row in results: self.id = '仓库编号:' + row[0] self.name = '货物名称:' + row[1] self.num = '货物数量:' + row[2] self.gid = '供应商编号:' + row[3] db.commit() # 提交到数据库执行 label = tk.Label(self.window, text='货物信息查看', bg='SkyBlue', font=('楷体', 20), width=70, height=2) label.pack(pady=20) Label(self.window, text=self.id, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.name, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.num, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.gid, font=('楷体', 18)).pack(pady=5) Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=12), command=self.back_1).pack( pady=150) self.window.protocol("WM_DELETE_WINDOW", self.back_1) self.window.mainloop() except: db.rollback() # 发生错误时回滚 messagebox.showinfo('提示', '数据库连接失败!') db.close() # 关闭数据库连接 else: messagebox.showinfo('提示', '请填写货物信息!') def back_1(self): cangkucha(self.window)# 出库class chuku: def __init__(self, parent_window): parent_window.destroy() # 销毁子界面 self.window = tk.Tk() self.window.title('出库表') self.window.geometry('700x600+70+50') self.top_title = Label(self.window, text='出库', bg='SkyBlue', font=('楷体', 20), width=70, height=2) self.top_title.pack() self.var_id = StringVar() # 声明 self.var_name = StringVar() # 声明 self.var_gender = StringVar() # 声明 self.var_age = StringVar() # 声明 self.right_top_id_label = Label(text="出库单号", font=('楷体', 15)).pack(pady=15) self.right_top_id_entry = Entry(textvariable=self.var_id, font=('楷体', 15)).pack() self.right_top_name_label = Label(text="货物编号", font=('楷体', 15)).pack(pady=15) self.right_top_name_entry = Entry(textvariable=self.var_name, font=('楷体', 15)).pack() self.right_top_gender_label = Label(text="出库量", font=('楷体', 15)).pack(pady=15) self.right_top_gender_entry = Entry(textvariable=self.var_gender, font=('楷体', 15)).pack() self.right_top_gender_label = Label(text="出库货物", font=('楷体', 15)).pack(pady=15) self.right_top_gender_entry = Entry(textvariable=self.var_age, font=('楷体', 15)).pack() self.right_top_button1 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30) self.right_top_button2 = ttk.Button(text='返回', width=20, command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) # 捕捉右上角关闭点击 self.id = [] self.name = [] self.gender = [] self.age = [] # 打开数据库连接 db = pymysql.connect("localhost", "root", "dudu010112", "仓库管理系统2") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "SELECT * FROM 出库" # SQL 查询语句 try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: self.id.append(row[0]) self.name.append(row[1]) self.gender.append(row[2]) self.age.append(row[3]) except: print("Error: unable to fetch data") messagebox.showinfo('提示', '数据库连接失败!') db.close() # 关闭数据库连接 def back(self): StartPage(self.window) # 显示主窗口 销毁本窗口 def new_row(self): if self.var_id.get() != '' and self.var_name.get() != '': db = pymysql.connect("localhost", "root", "dudu010112", "仓库管理系统2") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "INSERT INTO 出库(出库单号,货物编号,出库量,出库货物) VALUES ('%s', '%s','%s', '%s')" % \ (self.var_id.get(), self.var_name.get(),self.var_gender.get(),self.var_age.get()) # SQL 插入语句 try: cursor.execute(sql) # 执行sql语句 db.commit() # 提交到数据库执行 messagebox.showinfo('提示!', '出库成功!') except: db.rollback() # 发生错误时回滚 messagebox.showinfo('警告!', '数据库连接失败!') db.close() # 关闭数据库连接 else: messagebox.showinfo('警告!', '填写出库信息')if __name__ == '__main__': window = tk.Tk() StartPage(window)
界面样式
主界面功能可以自己添加
功能界面不在一一展示还有MySQL对应文件 这里我不太会发 可以私信我,也可以自己按照对数据库的连接自己做,很简单的,如果觉得有用请点一个赞吧,谢谢
转载地址:http://zycki.baihongyu.com/