项目名称:Tkinter小程序开发 2.项目描述: 利用Tkinter做一个简单的GUI应用小程序,实现一个特 定的功能、比如做一个计算器、学生信息管理系统等。 3.项目要求: 1.使用Tkinter创建用户界面,包括标签、文本框、按钮、菜单等控件。 2.使用面向对象编程的思想设计代码结构, 3.代码量:>100行
时间: 2024-03-20 14:42:36 浏览: 64
Tkinter-GUI-Application-Development-Cookbook:Tkinter GUI应用程序开发手册,由Packt发行
好的,以下是一个学生信息管理系统的示例代码,它包括学生信息的录入、查询、修改和删除等功能:
```python
import tkinter as tk
class Student:
def __init__(self, name, age, gender, score):
self.name = name
self.age = age
self.gender = gender
self.score = score
class StudentManagementSystem:
def __init__(self, master):
self.master = master
master.title("Student Management System")
# 创建标签和文本框
tk.Label(master, text="Name: ").grid(row=0, column=0, padx=5, pady=5)
self.name_entry = tk.Entry(master, width=30)
self.name_entry.grid(row=0, column=1, padx=5, pady=5)
tk.Label(master, text="Age: ").grid(row=1, column=0, padx=5, pady=5)
self.age_entry = tk.Entry(master, width=30)
self.age_entry.grid(row=1, column=1, padx=5, pady=5)
tk.Label(master, text="Gender: ").grid(row=2, column=0, padx=5, pady=5)
self.gender_entry = tk.Entry(master, width=30)
self.gender_entry.grid(row=2, column=1, padx=5, pady=5)
tk.Label(master, text="Score: ").grid(row=3, column=0, padx=5, pady=5)
self.score_entry = tk.Entry(master, width=30)
self.score_entry.grid(row=3, column=1, padx=5, pady=5)
# 创建按钮
tk.Button(master, text="Add", width=10, command=self.add_student).grid(row=4, column=0, padx=5, pady=5)
tk.Button(master, text="Search", width=10, command=self.search_student).grid(row=4, column=1, padx=5, pady=5)
tk.Button(master, text="Update", width=10, command=self.update_student).grid(row=4, column=2, padx=5, pady=5)
tk.Button(master, text="Delete", width=10, command=self.delete_student).grid(row=4, column=3, padx=5, pady=5)
# 创建菜单
menubar = tk.Menu(master)
master.config(menu=menubar)
file_menu = tk.Menu(menubar)
menubar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Exit", command=master.quit)
# 创建学生列表框
tk.Label(master, text="Student List:").grid(row=5, column=0, padx=5, pady=5)
self.student_listbox = tk.Listbox(master, width=50)
self.student_listbox.grid(row=6, column=0, columnspan=4, padx=5, pady=5)
# 初始化学生列表
self.students = []
self.refresh_student_list()
def add_student(self):
name = self.name_entry.get()
age = int(self.age_entry.get())
gender = self.gender_entry.get()
score = float(self.score_entry.get())
student = Student(name, age, gender, score)
self.students.append(student)
self.refresh_student_list()
self.name_entry.delete(0, tk.END)
self.age_entry.delete(0, tk.END)
self.gender_entry.delete(0, tk.END)
self.score_entry.delete(0, tk.END)
def search_student(self):
name = self.name_entry.get()
for i, student in enumerate(self.students):
if student.name == name:
self.student_listbox.selection_clear(0, tk.END)
self.student_listbox.selection_set(i)
self.student_listbox.activate(i)
break
def update_student(self):
selected_indices = self.student_listbox.curselection()
if len(selected_indices) == 0:
return
index = selected_indices[0]
name = self.name_entry.get()
age = int(self.age_entry.get())
gender = self.gender_entry.get()
score = float(self.score_entry.get())
student = Student(name, age, gender, score)
self.students[index] = student
self.refresh_student_list()
def delete_student(self):
selected_indices = self.student_listbox.curselection()
if len(selected_indices) == 0:
return
index = selected_indices[0]
del self.students[index]
self.refresh_student_list()
def refresh_student_list(self):
self.student_listbox.delete(0, tk.END)
for student in self.students:
self.student_listbox.insert(tk.END, f"{student.name} ({student.age}, {student.gender}, {student.score})")
root = tk.Tk()
system = StudentManagementSystem(root)
root.mainloop()
```
这个学生信息管理系统有一个界面,包括四个文本框和四个按钮,用于录入、查询、修改和删除学生信息。它还有一个学生列表框,用于显示学生信息。你可以根据这个示例代码,自己编写一个 GUI 应用小程序。
阅读全文