python 增刪改查 跳出視窗
时间: 2024-01-02 17:23:06 浏览: 139
以下是一个示例代码,演示了如何在Python中实现增删改查操作,并在需要时弹出窗口进行输入和输出:
```python
import tkinter as tk
from tkinter import messagebox
# 创建一个空字典用于存储学员信息
all_dict = {}
# 添加学员信息
def add_student():
num = input("请输入学号:")
name = input("请输入姓名:")
age = int(input("请输入年龄:"))
python = int(input("请输入Python成绩:"))
all_dict[num] = {'num': num, 'name': name, 'age': age, 'python': python}
messagebox.showinfo("提示", "添加成功")
# 删除学员信息
def delete_student():
num = input("请输入要删除的学号:")
if num in all_dict:
del all_dict[num]
messagebox.showinfo("提示", "删除成功")
else:
messagebox.showinfo("提示", "该学员不存在")
# 修改学员信息
def modify_student():
num = input("请输入要修改的学号:")
if num in all_dict:
name = input("请输入新的姓名:")
age = int(input("请输入新的年龄:"))
python = int(input("请输入新的Python成绩:"))
all_dict[num] = {'num': num, 'name': name, 'age': age, 'python': python}
messagebox.showinfo("提示", "修改成功")
else:
messagebox.showinfo("提示", "该学员不存在")
# 查看单个学员信息
def one_look():
one_num = input("请输入需要查询学员的学号:")
if one_num in all_dict:
student = all_dict[one_num]
messagebox.showinfo("查询结果", "学号: {}\n姓名: {}\n年龄: {}\nPython成绩: {}".format(student['num'], student['name'], student['age'], student['python']))
else:
messagebox.showinfo("提示", "该学员不存在")
# 查看考试的及格率
def pass_rate():
scores = [student['python'] for student in all_dict.values()]
pass_count = sum(score >= 60 for score in scores)
pass_rate = pass_count / len(all_dict) * 100
messagebox.showinfo("及格率", "本次python考试的及格率: {:.2f}%".format(pass_rate))
# 创建主窗口
window = tk.Tk()
window.title("学员管理系统")
# 创建按钮
add_button = tk.Button(window, text="添加学员", command=add_student)
delete_button = tk.Button(window, text="删除学员", command=delete_student)
modify_button = tk.Button(window, text="修改学员信息", command=modify_student)
look_button = tk.Button(window, text="查看单个学员信息", command=one_look)
pass_button = tk.Button(window, text="查看考试的及格率", command=pass_rate)
# 显示按钮
add_button.pack()
delete_button.pack()
modify_button.pack()
look_button.pack()
pass_button.pack()
# 运行主窗口
window.mainloop()
```
阅读全文