代码如下:
from tkinter import * from tkinter import ttk from tkinter.messagebox import showinfo list1 = [ ] # 添加学生信息 def add_student(): if(stu_name.get() != ""): # print(stu_name.get()) infor = [ stu_name.get(), stu_num.get(), stu_sex.get(), stu_age.get(), stu_Phone.get() ] list1.append(infor) showinfo("提示信息", message="添加学生信息成功!") else: stu_name.delete(0, END) showinfo("提示信息", message="学生信息为空!请输入要添加的学生信息!") del_info() # 删除输入框内容 disp_student() # 在列表框更新学生信息 # 删除输入框内信息 def del_info(): stu_name.delete(0, 'end') stu_num.delete(0, 'end') stu_sex.delete(0, 'end') stu_age.delete(0, 'end') stu_Phone.delete(0, 'end') # 删除学生信息,按学号删除 def del_student(): i = stu_name.get() k = 0 for j in list1: if j[ 0 ] == i: del list1[ k ] break k += 1 print(list1) stu_name.delete(0, 'end') del_info() # 删除输入框内容 disp_student() # 更新下面列表框显示 # 修改学生信息 def change_student(): i = stu_name.get() k = 0 for j in list1: if j[ 0 ] == i: j[ 1 ] = stu_num.get() j[ 2 ] = stu_sex.get() j[ 3 ] = stu_age.get() j[ 4 ] = stu_Phone.get() break k += 1 print(list1) stu_name.delete(0, 'end') stu_num.delete(0, 'end') stu_sex.delete(0, 'end') stu_age.delete(0, 'end') stu_Phone.delete(0, 'end') disp_student() # 显示学生信息 def disp_student(): tree = ttk.Treeview(master=a, height=6, show='headings', ) tree[ 'columns' ] = [ 'name', 'xh', 'sex', 'age', 'ph' ] tree.place(x=34, y=220) tree.column('name', width=70) tree.column('xh', width=70) tree.column('sex', width=50) tree.column('age', width=50) tree.column('ph', width=100) tree.heading('name', text='姓名') tree.heading('xh', text='学号') tree.heading('sex', text='性别') tree.heading('age', text='年龄') tree.heading('ph', text='电话号码') for index, data in enumerate(list1): tree.insert('', 'end', values=data) a.mainloop() # 界面设计 a = Tk() a.title('学生信息系统') a.geometry('400x380+50+10') L1 = Label(a, height=2, text='学生信息管理系统', bg='lightblue') L1.pack(fill='x') ad = Button(a, text='添加学生信息', command=add_student) ad.place(x=30, y=60, width=130, height=20) sc = Button(a, text='删除学生信息', command=del_student) sc.place(x=30, y=90, width=130, height=20) xg = Button(a, text='修改学生信息', command=change_student) xg.place(x=30, y=120, width=130, height=20) xs = Button(a, text='显示学生信息', command=disp_student) xs.place(x=30, y=150, width=130, height=20) label_name = Label(a, text='姓名:') label_name.place(x=200, y=50, width=40, height=40) stu_name = Entry(a) stu_name.place(x=240, y=60, width=130, height=20) label_num = Label(a, text='学号:') label_num.place(x=200, y=80, width=40, height=40) stu_num = Entry(a) stu_num.place(x=240, y=90, width=130, height=20) label_sex = Label(a, text='性别:') label_sex.place(x=200, y=110, width=40, height=40) stu_sex = Entry(a) stu_sex.place(x=240, y=120, width=130, height=20) label_age = Label(a, text='年龄:') label_age.place(x=200, y=140, width=40, height=40) stu_age = Entry(a) stu_age.place(x=240, y=150, width=130, height=20) label_phone = Label(a, text='电话:') label_phone.place(x=200, y=170, width=40, height=40) stu_Phone = Entry(a) stu_Phone.place(x=240, y=180, width=130, height=20) def main(): disp_student() a.mainloop() main()
这里的代码写了一个使用Tkinter库实现的学生管理系统应用程序。这个程序允许用户添加、删除、修改和显示学生信息,用thinker模块进行界面的排版,与设计
在上述写的过程中可能会出现的相关问题:
-
布局问题
- 界面布局使用了绝对位置和大小,这可能导致在不同分辨率的屏幕上显示效果不一致。可以考虑使用
grid
或pack
布局管理器进行改进。(注意:grid 或 pack 只能使用其一,不能同时使用)
- 界面布局使用了绝对位置和大小,这可能导致在不同分辨率的屏幕上显示效果不一致。可以考虑使用
-
事件处理
- 需要确保每个按钮的命令函数正确无误。如果某个按钮的命令函数设置错误,可能会导致功能无法正常工作。
-
数据持久化
- 目前学生信息仅保存在内存中,程序关闭后数据会丢失。可以考虑使用文件或数据库来存储数据,以实现数据的持久化。
-
输入验证
- 需要添加输入验证,确保用户输入的数据合法。例如,学号应为数字,年龄应为正整数,电话号码应符合格式要求。
-
错误处理
- 需要处理各种可能的错误情况,如删除不存在的学生、修改不存在的学生信息等,并向用户显示友好的错误信息。
-
更新显示
- 每次更新学生信息时都会创建新的
Treeview
,这可能导致内存泄漏和性能问题。可以考虑在创建一次Treeview
后,仅更新其内容。
- 每次更新学生信息时都会创建新的
下面给予用grid 布局优化后的代码:
from tkinter import *
from tkinter import ttk
from tkinter.messagebox import showinfo
list1 = []
def add_student():
if stu_name.get():
infor = [stu_name.get(), stu_num.get(), stu_sex.get(), stu_age.get(), stu_phone.get()]
list1.append(infor)
showinfo("提示信息", message="添加学生信息成功!")
del_info()
disp_student()
else:
showinfo("提示信息", message="学生信息为空!请输入要添加的学生信息!")
def del_info():
stu_name.delete(0, END)
stu_num.delete(0, END)
stu_sex.delete(0, END)
stu_age.delete(0, END)
stu_phone.delete(0, END)
def del_student():
student_name = stu_name.get()
global list1
list1 = [student for student in list1 if student[0] != student_name]
showinfo("提示信息", message="删除学生信息成功!")
del_info()
disp_student()
def change_student():
student_name = stu_name.get()
for student in list1:
if student[0] == student_name:
student[1] = stu_num.get()
student[2] = stu_sex.get()
student[3] = stu_age.get()
student[4] = stu_phone.get()
showinfo("提示信息", message="修改学生信息成功!")
break
else:
showinfo("提示信息", message="学生信息未找到!")
del_info()
disp_student()
def disp_student():
for row in tree.get_children():
tree.delete(row)
for student in list1:
tree.insert('', 'end', values=student)
a = Tk()
a.title('学生信息系统')
a.geometry('500x400')
frame = Frame(a)
frame.pack(pady=20)
label_title = Label(frame, text='学生信息管理系统', bg='lightblue', font=("黑体", 16))
label_title.grid(row=0, column=0, columnspan=2, sticky="ew")
buttons_frame = Frame(frame)
buttons_frame.grid(row=1, column=0, columnspan=2, pady=10)
btn_add = Button(buttons_frame, text='添加学生信息', command=add_student)
btn_add.grid(row=0, column=0, padx=5)
btn_del = Button(buttons_frame, text='删除学生信息', command=del_student)
btn_del.grid(row=0, column=1, padx=5)
btn_change = Button(buttons_frame, text='修改学生信息', command=change_student)
btn_change.grid(row=0, column=2, padx=5)
btn_show = Button(buttons_frame, text='显示学生信息', command=disp_student)
btn_show.grid(row=0, column=3, padx=5)
entries_frame = Frame(frame)
entries_frame.grid(row=2, column=0, columnspan=2, pady=10)
label_name = Label(entries_frame, text='姓名:')
label_name.grid(row=0, column=0, padx=5, pady=5)
stu_name = Entry(entries_frame)
stu_name.grid(row=0, column=1, padx=5, pady=5)
label_num = Label(entries_frame, text='学号:')
label_num.grid(row=1, column=0, padx=5, pady=5)
stu_num = Entry(entries_frame)
stu_num.grid(row=1, column=1, padx=5, pady=5)
label_sex = Label(entries_frame, text='性别:')
label_sex.grid(row=2, column=0, padx=5, pady=5)
stu_sex = Entry(entries_frame)
stu_sex.grid(row=2, column=1, padx=5, pady=5)
label_age = Label(entries_frame, text='年龄:')
label_age.grid(row=3, column=0, padx=5, pady=5)
stu_age = Entry(entries_frame)
stu_age.grid(row=3, column=1, padx=5, pady=5)
label_phone = Label(entries_frame, text='电话:')
label_phone.grid(row=4, column=0, padx=5, pady=5)
stu_phone = Entry(entries_frame)
stu_phone.grid(row=4, column=1, padx=5, pady=5)
tree_frame = Frame(frame)
tree_frame.grid(row=3, column=0, columnspan=2, pady=10)
tree = ttk.Treeview(tree_frame, columns=('name', 'xh', 'sex', 'age', 'ph'), show='headings')
tree.heading('name', text='姓名')
tree.heading('xh', text='学号')
tree.heading('sex', text='性别')
tree.heading('age', text='年龄')
tree.heading('ph', text='电话号码')
tree.column('name', width=100)
tree.column('xh', width=100)
tree.column('sex', width=50)
tree.column('age', width=50)
tree.column('ph', width=120)
tree.pack()
a.mainloop()