最近有不少小伙伴问我,Python 怎么学,我的统一回答:就是实战,多练。无论做什么,都逃不过熟能生巧。其次就是从自己的兴趣出发,做一些实战小项目。往往一些小项目都藏着很多基础,这周在家闲着的时候给大家用Python写了一个疫苗管理系统的小项目。很适合新手练习,主要涉及的知识有Python、tkinter、数据库存储等。(https://jq.qq.com/?_wv=1027&k=EBXZ9r6u)
整体结构图
连接数据库
def connect_DBS(self, database, content): db = pymysql.connect(host=\"localhost\", user=\"root\", password=\"pwd\", database=database) cursor = db.cursor() cursor.execute(content) data = cursor.fetchone() db.commit() db.close() return data
主界面
def main_window(self): tk.Button(app, text=\'登录\', bg=\'white\', font=(\"Arial,12\"), width=12, height=1, command=self.login).place(x=260, y=200) tk.Button(app, text=\'注册\', bg=\'white\', font=(\"Arial,12\"), width=12, height=1, command=self.register).place(x=260, y=240) tk.Button(app, text=\'退出\', bg=\'white\', font=(\"Arial,12\"), width=12, height=1, command=self.quit_mainloop).place(x=260, y=280)
注册界面
# def register(self): register = tk.Toplevel(app) register.title(\'用户注册\') register.geometry(\"600x400\") tk.Label(register, text=\"欢迎注册\", font=(\"KaiTi\", 40)).place(x=200, y=20) tk.Label(register, text=\'添加管理员姓名:\', font=(\"Arial\", 9)).place(x=80, y=120) tk.Label(register, text=\'确认管理员编号:\', font=(\'Arial\', 9)).place(x=80, y=150) entry1 = tk.Entry(register, font=(\"Arial, 9\"), width=46, ) entry2 = tk.Entry(register, font=(\"Arial, 9\"), width=46, ) entry1.pack() entry2.pack() entry1.place(x=180, y=120, width=350, height=25) entry2.place(x=180, y=150, width=350, height=25) def user_register(): user_name = entry1.get() user_code = entry2.get() if user_name == \"\" or user_code == \"\": tkinter.messagebox.showwarning(title=\"警告\", message=\"用户名或密码不能为空!\") else: content = \"INSERT INTO user_info (user_name, user_code) VALUES (\'%s\', \'%s\');\" % (user_name, user_code) self.connect_DBS(database=\"vaccine_info\", content=content) tkinter.messagebox.showinfo(title=\"信息\", message=\"注册成功!\") tk.Button(register, text=\"注册\", bg=\'white\', font=(\"Arial,9\"), width=12, height=0, command=user_register).place(x=250, y=250)
登陆界面
# def login(self): login = tk.Toplevel(app) login.title(\'用户登录\') login.geometry(\"600x400\") tk.Label(login, text=\"欢迎登录\", font=(\"KaiTi\", 40)).place(x=200, y=20) tk.Label(login, text=\'管理员姓名:\', font=(\"Arial\", 9)).place(x=80, y=120) tk.Label(login, text=\'管理员编号:\', font=(\'Arial\', 9)).place(x=80, y=150) entry1 = tk.Entry(login, font=(\"Arial, 9\"), width=46) entry2 = tk.Entry(login, font=(\"Arial, 9\"), width=46, show=\"*\") entry1.pack() entry2.pack() entry1.place(x=180, y=120, width=350, height=25) entry2.place(x=180, y=150, width=350, height=25) def user_check(): user_name = entry1.get() user_code = entry2.get() content = \"SELECT * FROM user_info WHERE user_name = \'%s\';\" % user_name data = self.connect_DBS(database=\"vaccine_info\", content=content) try: if user_name == data[1] and user_code == data[2]: tkinter.messagebox.showinfo(title=\"信息\", message=\"欢迎登录!\") self.options() elif user_name != data[1]: tkinter.messagebox.showerror(title=\"错误\", message=\"请注册后再进行登录!\") elif user_name == data[1] and user_code != data[2]: tkinter.messagebox.showerror(title=\"错误\", message=\"密码错误!\") except TypeError: tkinter.messagebox.showerror(title=\"错误\", message=\"请注册后再进行登录!\") tk.Button(login, text=\"登录\", bg=\'white\', font=(\"Arial,9\"), width=12, height=0, command=user_check).place(x=250, y=250)