1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| import tkinter as tk from tkinter import messagebox import pymysql
class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.master.title("学生信息录入") self.pack()
self.name_label = tk.Label(self, text="姓名:") self.name_label.grid(row=0, column=0) self.name_entry = tk.Entry(self) self.name_entry.grid(row=0, column=1) self.gender_label = tk.Label(self, text="性别:") self.gender_label.grid(row=1, column=0) self.gender_entry = tk.Entry(self) self.gender_entry.grid(row=1, column=1)
self.age_label = tk.Label(self, text="年龄:") self.age_label.grid(row=2, column=0) self.age_entry = tk.Entry(self) self.age_entry.grid(row=2, column=1)
self.grade_label = tk.Label(self, text="成绩:") self.grade_label.grid(row=3, column=0) self.grade_entry = tk.Entry(self) self.grade_entry.grid(row=3, column=1)
self.submit_button = tk.Button(self, text="提交", command=self.submit_data) self.submit_button.grid(row=4, column=0, columnspan=1) self.show_data_button = tk.Button(self, text="显示数据", command=self.show_data) self.show_data_button.grid(row=4, column=1, columnspan=1)
def submit_data(self): conn = pymysql.connect( host='localhost', user='roroshen', password='xxxxxx', database='miaomiao')
sql = "INSERT INTO students (name, gender, age, grade) VALUES (%s, %s, %s, %s)"
cursor = conn.cursor()
name = self.name_entry.get() age = self.age_entry.get() gender = self.gender_entry.get() grade = self.grade_entry.get() cursor.execute(sql, (name, gender, age, grade)) conn.commit()
cursor.close() conn.close() messagebox.showinfo("提交成功", "您的信息已经成功提交!")
self.name_entry.delete(0, tk.END) self.gender_entry.delete(0, tk.END) self.age_entry.delete(0, tk.END) self.grade_entry.delete(0, tk.END) def show_data(self): conn = pymysql.connect( host='localhost', user='roroshen', password='xxxxxx', database='miaomiao')
sql = "SELECT * FROM students"
cursor = conn.cursor()
cursor.execute(sql) rows = cursor.fetchall()
cursor.close() conn.close()
result_window = tk.Toplevel(self) result_window.title("学生信息") result_listbox = tk.Listbox(result_window, width=60) result_listbox.insert(0, "姓名 性别 年龄 成绩") for row in rows: result_listbox.insert(tk.END, f"{row[0]} {row[1]} {row[2]} {row[3]}") result_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) xscrollbar = tk.Scrollbar(result_window, orient=tk.HORIZONTAL) xscrollbar.pack(side=tk.BOTTOM, fill=tk.X) result_listbox.config(xscrollcommand=xscrollbar.set) xscrollbar.config(command=result_listbox.xview)
root = tk.Tk() app = Application(master=root) app.mainloop()
|