Python 猪群管理系统:GUI界面设计与数据库操作
"def herd_manage(self):\n self.win = Tk()\n self.herdid_t = tk.StringVar()\n self.herdname_t = tk.StringVar()\n self.creattime_t = tk.StringVar()\n self.win.title('猪群管理界面')\n self.win.geometry('800x400')\n Label(self.win,text='猪群id').place(x=50, y=50)\n self.herdid = Entry(self.win,textvariable=self.herdid_t)\n self.herdid.place(x=100, y=50)\n Label(self.win,text='猪群名称').place(x=400, y=50)\n self.herdname = Entry(self.win,textvariable=self.herdname_t)\n self.herdname.place(x=450, y=50)\n Label(self.win,text='创建时间').place(x=50, y=100)\n self.creattime = Entry(self.win,textvariable=self.creattime_t)\n self.creattime.place(x=100, y=100)\n # self.herdid_t=self.herdid.get()\n # self.herdname_t=self.herdname.get()\n # self.creattime_t=self.creattime.get()\n Button(self.win, text='查找', command=self.select).place(x=100, y=150)\n Button(self.win, text='添加', command=self.add).place(x=250, y=150)\n Button(self.win, text='删除', command=self.delect).place(x=400, y=150)\n Button(self.win, text='修改', command=self.upgrade).place(x=550, y=150)\n self.result_label = tk.Label(self.win,text='')\n self.result_label.pack(x=200,y=300)\n self.win.mainloop()\n def select(self):\n db = pymysql.connect(host='localhost', user='root', password='111111', port=3306, db='student', charset='utf8')\n self.cursor = db.cursor()\n sql = f"SELECT * FROM pigherd WHERE herdid='{self.herdid_t.get()}'' OR herdname='{self.herdname_t.get()}'' OR creattime='{self.creattime_t.get()}'"\n self.cursor.execute(sql)\n result = self.cursor.fetchone()\n self.result_label.config(text=result)\n db.close()\n检查代码\n"The code appears to be a class method that manages a GUI interface for managing pig herds. The method creates a window using the Tkinter library, sets up various labels and entry fields for input, and adds buttons for performing actions such as searching, adding, deleting, and updating herd information. \n\nIn the select
method, a database connection is established using pymysql and a SQL query is executed based on the provided input. The result of the query is fetched and displayed in a label widget.\n\nThe code looks fine, but there are a few things to note:\n\n1. The upgrade
method is called immediately when setting the command for the "修改" button. You should remove the parentheses after upgrade
to assign the method itself as the command, rather than calling it immediately.\n\n2. The code does not handle any exceptions that may occur during the database operations. It would be a good idea to wrap the database operations in try-except blocks to handle potential errors and provide appropriate error messages to the user.\n\n3. The code could benefit from better variable naming. For example, instead of self.win
, a more descriptive name like self.window
could be used. Similarly, using more meaningful names for the labels, buttons, and entry fields would make the code more readable.\n\nOverall, the code appears to be functional, but there are a few areas that could be improved for better error handling and code readability.\n"
原文地址: http://www.cveoy.top/t/topic/pNCH 著作权归作者所有。请勿转载和采集!