Python Tkinter 数据库查询 - 使用 pymysql 连接 MySQL 并显示结果
import\ tkinter\ as\ tk\nimport\ pymysql\n\nclass\ MyApp:\n\tdef\ init(self):\n\t self.window\ =\ tk.Tk()\n\t self.herdid_t\ =\ tk.StringVar()\n\t self.herdname_t\ =\ tk.StringVar()\n\t self.creattime_t\ =\ tk.StringVar()\n\n\t tk.Label(self.window,\ text="herdid").grid(row=0,\ column=0)\n\t tk.Label(self.window,\ text="herdname").grid(row=1,\ column=0)\n\t tk.Label(self.window,\ text="creattime").grid(row=2,\ column=0)\n\n\t self.herdid_entry\ =\ tk.Entry(self.window,\ textvariable=self.herdid_t)\n\t self.herdid_entry.grid(row=0,\ column=1)\n\t self.herdname_entry\ =\ tk.Entry(self.window,\ textvariable=self.herdname_t)\n\t self.herdname_entry.grid(row=1,\ column=1)\n\t self.creattime_entry\ =\ tk.Entry(self.window,\ textvariable=self.creattime_t)\n\t self.creattime_entry.grid(row=2,\ column=1)\n\n\t self.select_button\ =\ tk.Button(self.window,\ text="查询",\ command=self.select)\n\t self.select_button.grid(row=3,\ columnspan=2)\n\n\t self.result_label\ =\ tk.Label(self.window,\ text="")\n\t self.result_label.grid(row=4,\ columnspan=2)\n\n\tdef\ select(self):\n\t db\ =\ pymysql.connect(host='localhost',\ user='root',\ password='111111',\ port=3306,\ db='student',\ charset='utf8')\n\t cursor\ =\ db.cursor()\n\t sql\ =\ f"SELECT\ *\ FROM\ pigherd\ WHERE\ herdid='{self.herdid_t.get()}'\ OR\ herdname='{self.herdname_t.get()}'\ OR\ creattime='{self.creattime_t.get()}'"\n\t cursor.execute(sql)\n\t result\ =\ cursor.fetchone()\n\t self.result_label.config(text=result)\n\t db.close()\n\n\tdef\ run(self):\n\t self.window.mainloop()\n\napp\ =\ MyApp()\napp.run()\n
原文地址: https://www.cveoy.top/t/topic/pNhR 著作权归作者所有。请勿转载和采集!