# 修改学生信息def update_student_info_window(): tk.Label(root, text="更新学生信息").pack() tk.Entry(root, textvariable=tk.StringVar(value='请输入学生信息')).pack() tk.Button(root, text="更新", command=lambda: update_student()).pack()def update_student(): id = id_entry.get() name = name_entry.get() age = age_entry.get() gender = gender_entry.get() IDcard = IDcard_entry.get() xueyuan = xueyuan_entry.get() banji = banji_entry.get() zhuanye = zhuanye_entry.get() jiguan = jiguan_entry.get() number = number_entry.get() sql = "UPDATE students SET name = %s, age = %s, gender = %s, IDcard = %s, xueyuan = %s, banji = %s, zhuanye = %s, jiguan = %s, number = %s WHERE id = %s" val = (name, age, gender, IDcard, xueyuan, banji, zhuanye, jiguan, number, id) cursor.execute(sql, val) mydb.commit() id_entry.delete(0, tk.END) name_entry.delete(0, tk.END) age_entry.delete(0, tk.END) gender_entry.delete(0, tk.END) IDcard_entry.delete(0, tk.END) xueyuan_entry.delete(0, tk.END) banji_entry.delete(0, tk.END) zhuanye_entry.delete(0, tk.END) jiguan_entry.delete(0, tk.END) number_entry.delete(0, tk.END)# 删除学生信息def delete_student_info_window(): tk.Label(root, text="删除学生信息").pack() tk.Entry(root, textvariable=tk.StringVar(value='请输入学生 ID')).pack() tk.Button(root, text="删除", command=lambda: delete_student()).pack()def delete_student(): id = id_entry.get() sql = "DELETE FROM students WHERE id = %s" val = (id,) cursor.execute(sql, val) mydb.commit() id_entry.delete(0, tk.END) name_entry.delete(0, tk.END) age_entry.delete(0, tk.END) gender_entry.delete(0, tk.END) IDcard_entry.delete(0, tk.END) xueyuan_entry.delete(0, tk.END) banji_entry.delete(0, tk.END) zhuanye_entry.delete(0, tk.END) jiguan_entry.delete(0, tk.END) number_entry.delete(0, tk.END)
时间: 2024-04-03 18:30:54 浏览: 78
这段代码看起来是一个学生信息管理系统的界面和相关操作函数,其中包括添加、修改、删除学生信息的窗口和对应的操作函数。在更新学生信息的操作函数中,通过获取输入框中的各个字段值和待更新的学生ID,使用SQL语句更新数据库中对应学生的信息。在删除学生信息的操作函数中,通过获取输入框中的待删除学生ID,使用SQL语句从数据库中删除对应的学生信息。
相关问题
def update_student_info_window(): tk.Label(root, text="更新学生信息").pack() tk.Entry(root, textvariable=tk.StringVar(value='请输入学生信息')).pack() tk.Button(root, text="更新", command=lambda: update_student_info(cursor, cnx)).pack()解释代码
这段代码是用来创建一个 Tkinter 窗口,其中包括一个标签("更新学生信息")、一个文本框(用于输入学生信息)和一个按钮("更新")。当用户点击按钮时,会调用 `update_student_info` 函数,并将 `cursor` 和 `cnx` 作为参数传递给它。`cursor` 和 `cnx` 是用于与数据库进行交互的对象,`update_student_info` 函数会根据用户输入的学生信息更新数据库中对应的记录。
用python编写一个程序,其中包括一个菜单和一个多选列表框。菜单应该包含两个选项:添加学生和删除学生。当用户选择添加学生选项时,程序应该弹出一个对话框,要求用户输入学生的姓名、年龄和成绩。当用户选择删除学生选项时,程序应该从列表框中删除当前选定的学生。同时,当用户在列表框中选择一个学生时,程序应该显示该学生的详细信息
好的,以下是Python代码实现:
```python
import tkinter as tk
from tkinter import messagebox
class Student:
def __init__(self, name, age, score):
self.name = name
self.age = age
self.score = score
def __str__(self):
return f"{self.name} ({self.age} years old) - Score: {self.score}"
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, name, age, score):
student = Student(name, age, score)
self.students.append(student)
def remove_student(self, index):
del self.students[index]
class GUI:
def __init__(self):
self.sms = StudentManagementSystem()
self.root = tk.Tk()
self.root.title("Student Management System")
self.menu = tk.Menu(self.root)
self.root.config(menu=self.menu)
self.file_menu = tk.Menu(self.menu)
self.menu.add_cascade(label="File", menu=self.file_menu)
self.file_menu.add_command(label="Add Student", command=self.add_student)
self.file_menu.add_command(label="Remove Student", command=self.remove_student)
self.student_listbox = tk.Listbox(self.root, selectmode=tk.SINGLE)
self.student_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
self.student_listbox.bind("<<ListboxSelect>>", self.show_student_details)
self.details_frame = tk.Frame(self.root)
self.details_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=1)
self.name_label = tk.Label(self.details_frame, text="Name:")
self.name_label.pack()
self.name_var = tk.StringVar()
self.name_entry = tk.Entry(self.details_frame, textvariable=self.name_var, state=tk.DISABLED)
self.name_entry.pack()
self.age_label = tk.Label(self.details_frame, text="Age:")
self.age_label.pack()
self.age_var = tk.StringVar()
self.age_entry = tk.Entry(self.details_frame, textvariable=self.age_var, state=tk.DISABLED)
self.age_entry.pack()
self.score_label = tk.Label(self.details_frame, text="Score:")
self.score_label.pack()
self.score_var = tk.StringVar()
self.score_entry = tk.Entry(self.details_frame, textvariable=self.score_var, state=tk.DISABLED)
self.score_entry.pack()
self.root.mainloop()
def add_student(self):
dialog = AddStudentDialog(self.root)
if dialog.result is not None:
name, age, score = dialog.result
self.sms.add_student(name, age, score)
self.update_student_listbox()
def remove_student(self):
selected_index = self.student_listbox.curselection()
if len(selected_index) == 1:
confirm = messagebox.askyesno("Confirm", "Are you sure you want to remove this student?")
if confirm:
self.sms.remove_student(selected_index[0])
self.update_student_listbox()
def show_student_details(self, event):
selected_index = self.student_listbox.curselection()
if len(selected_index) == 1:
student = self.sms.students[selected_index[0]]
self.name_var.set(student.name)
self.age_var.set(student.age)
self.score_var.set(student.score)
self.name_entry.config(state=tk.NORMAL)
self.age_entry.config(state=tk.NORMAL)
self.score_entry.config(state=tk.NORMAL)
else:
self.name_var.set("")
self.age_var.set("")
self.score_var.set("")
self.name_entry.config(state=tk.DISABLED)
self.age_entry.config(state=tk.DISABLED)
self.score_entry.config(state=tk.DISABLED)
def update_student_listbox(self):
self.student_listbox.delete(0, tk.END)
for student in self.sms.students:
self.student_listbox.insert(tk.END, str(student))
class AddStudentDialog:
def __init__(self, parent):
self.result = None
self.top = tk.Toplevel(parent)
self.name_label = tk.Label(self.top, text="Name:")
self.name_label.pack()
self.name_var = tk.StringVar()
self.name_entry = tk.Entry(self.top, textvariable=self.name_var)
self.name_entry.pack()
self.age_label = tk.Label(self.top, text="Age:")
self.age_label.pack()
self.age_var = tk.StringVar()
self.age_entry = tk.Entry(self.top, textvariable=self.age_var)
self.age_entry.pack()
self.score_label = tk.Label(self.top, text="Score:")
self.score_label.pack()
self.score_var = tk.StringVar()
self.score_entry = tk.Entry(self.top, textvariable=self.score_var)
self.score_entry.pack()
self.ok_button = tk.Button(self.top, text="OK", command=self.ok)
self.ok_button.pack()
self.top.bind("<Return>", lambda event: self.ok())
self.top.grab_set()
self.top.wait_window()
def ok(self):
self.result = (self.name_var.get(), int(self.age_var.get()), int(self.score_var.get()))
self.top.destroy()
if __name__ == "__main__":
gui = GUI()
```
这个程序包括一个 `Student` 类,用于表示学生的姓名、年龄和成绩,以及一个 `StudentManagementSystem` 类,用于管理学生。GUI 部分使用 `tkinter` 库,包括一个菜单和一个多选列表框。当用户选择添加学生选项时,程序弹出一个对话框,要求用户输入学生的姓名、年龄和成绩。当用户选择删除学生选项时,程序从列表框中删除当前选定的学生。同时,当用户在列表框中选择一个学生时,程序显示该学生的详细信息。
阅读全文