python tkinter 学生信息管理系统

使用tkinter模块,python3.6,主要功能有添加,查询,删除,修改学生信息

使用模版:

1 from tkinter import *
2 import tkinter.font as tkFont
3 import tkinter as tk
4 from tkinter import ttk

最主要也是最难做的是,实现不同功能的界面在同一TK窗口内容的转换,通过把每个界面做成一个Frame框架,用for循环转换界面,来解决这个问题

 1 LARGE_FONT= ("Verdana", 20)
 2 
 3 class Application(tk.Tk):
 4     def __init__(self):
 5         
 6         super().__init__()
 7 
 8         self.wm_title("学生信息管理系统")
 9         
10         container = tk.Frame(self)
11         container.pack(side="top", fill="both", expand = True)
12         container.grid_rowconfigure(0, weight=1)
13         container.grid_columnconfigure(0, weight=1)
14 
15         self.frames = {}
16         #循环功能界面
17         for F in (StartPage, PageOne, PageTwo, PageThree,PageFour):
18             frame = F(container, self)
19             self.frames[F] = frame
20             frame.grid(row=0, column=0, sticky="nsew")  # 四个页面的位置都是 grid(row=0, column=0), 位置重叠,只有最上面的可见!!
21 
22         self.show_frame(StartPage)
23 
24         
25     def show_frame(self, cont):
26         frame = self.frames[cont]
27         frame.tkraise() # 切换,提升当前 tk.Frame z轴顺序(使可见)!!此语句是本程序的点睛之处

下面贴出的是主界面,

                                       

这是主页面部分代码:

 1 #主页面
 2 class StartPage(tk.Frame):
 3     '''主页'''
 4     def __init__(self, parent, root):
 5         super().__init__(parent)
 6         label = tk.Label(self, text="学生信息管理系统", font=LARGE_FONT)
 7         label.pack(pady=100)
 8         ft2=tkFont.Font(size=16)
 9         Button(self, text="添加学生信息",font=ft2,command=lambda: root.show_frame(PageOne),width=30,height=2,fg='white',bg='gray',activebackground='black',activeforeground='white').pack()
10         Button(self, text="删除学生信息",font=ft2,command=lambda: root.show_frame(PageTwo),width=30,height=2).pack()
11         Button(self, text="修改学生信息",font=ft2,command=lambda: root.show_frame(PageThree),width=30,height=2,fg='white',bg='gray',activebackground='black',activeforeground='white').pack()
12         Button(self, text="查询学生信息",font=ft2,command=lambda: root.show_frame(PageFour),width=30,height=2).pack()
13         Button(self,text='退出系统',height=2,font=ft2,width=30,command=root.destroy,fg='white',bg='gray',activebackground='black',activeforeground='white').pack()

添加学生信息页面:

             

第二功能页面添加学生信息,,

 1 #添加学生信息
 2 class PageOne(tk.Frame):
 3     def __init__(self, parent, root):
 4         super().__init__(parent)
 5         label = tk.Label(self, text="添加学生信息", font=LARGE_FONT)
 6         label.pack(pady=100)
 7 
 8         ft3=tkFont.Font(size=14)
 9         ft4=tkFont.Font(size=12)
10         Label(self,text='学生学号:',font=ft3).pack(side=TOP)
11         global e1
12         e1=StringVar()
13         Entry(self,width=30,textvariable=e1,font=ft3,bg='Ivory').pack(side=TOP)
14         Label(self,text='学生姓名:',font=ft3).pack(side=TOP)
15         global e2
16         e2=StringVar()
17         Entry(self,width=30,textvariable=e2,font=ft3,bg='Ivory').pack(side=TOP)
18         Label(self,text='学生成绩:',font=ft3).pack(side=TOP)
19         global e3
20         e3=StringVar()
21         Entry(self,width=30,textvariable=e3,font=ft3,bg='Ivory').pack(side=TOP)
22         Button(self, text="返回首页",width=8,font=ft4,command=lambda: root.show_frame(StartPage)).pack(pady=20)
23         Button(self, text="确定保存",width=8,font=ft4,command=self.save).pack(side=TOP)
24         
25     def save(self):
26         with open('student_infor.txt','a+') as student_infor:
27             num=str(e1.get())
28             name=str(e2.get())
29             score=str(e3.get())
30             student_infor.write(num+' '+name+' '+score+'\n')

删除学生信息

              

 1 #删除学生信息
 2 class PageTwo(tk.Frame):
 3     def __init__(self, parent, root):
 4         super().__init__(parent)
 5         label = tk.Label(self, text="删除学生信息", font=LARGE_FONT)
 6         label.pack(pady=100)
 7 
 8         ft3=tkFont.Font(size=14)
 9         ft4=tkFont.Font(size=12)
10         Label(self,text='请输入你要删除的学生学号:',font=ft3).pack(side=TOP)
11         global e4
12         e4=StringVar()
13         Entry(self,width=30,textvariable=e4,font=ft3,bg='Ivory').pack(side=TOP)
14         Button(self, text="确定删除",width=8,font=ft4,command=self.del1).pack(pady=20)
15         Button(self, text="返回首页",width=8,font=ft4,command=lambda: root.show_frame(StartPage)).pack()
16         #button1 = ttk.Button(self, text="回到首页", command=lambda: root.show_frame(StartPage)).pack()
17         #button2 = ttk.Button(self, text="去到第一页", command=lambda: root.show_frame(PageOne)).pack()
18     def del1(self):
19         num2=str(e4.get())
20         with open('student_infor.txt','r') as f:
21             lines=f.readlines()
22             with open('student_infor.txt','w') as f_w:
23                 for line in lines:
24                     if num2 in line:
25                         continue
26                     f_w.write(line)

修改学生信息:

                

 1 #修改学生信息
 2 class PageThree(tk.Frame):
 3     def __init__(self, parent, root):
 4         super().__init__(parent)
 5         tk.Label(self, text="修改学生信息", font=LARGE_FONT).pack(pady=100)
 6 
 7         ft3=tkFont.Font(size=14)
 8         ft4=tkFont.Font(size=12)
 9         Label(self,text='请输入你要修改的学生学号:',font=ft3).pack(side=TOP)
10         self.e5=StringVar()
11         Entry(self,width=30,textvariable=self.e5,font=ft3,bg='Ivory').pack(side=TOP)
12         Label(self,text='学生姓名:',font=ft3).pack(side=TOP)
13         self.e6=StringVar()
14         Entry(self,width=30,textvariable=self.e6,font=ft3,bg='Ivory').pack(side=TOP)
15         Label(self,text='学生成绩:',font=ft3).pack(side=TOP)
16         self.e7=StringVar()
17         Entry(self,width=30,textvariable=self.e7,font=ft3,bg='Ivory').pack(side=TOP)
18         Button(self, text="确定修改",width=8,font=ft4,command=self.modify).pack(pady=20)
19         Button(self, text="返回首页",width=8,font=ft4,command=lambda: root.show_frame(StartPage)).pack()
20     def modify(self):
21         num3=str(self.e5.get())
22         name3=str(self.e6.get())
23         score3=str(self.e7.get())
24         with open('student_infor.txt','r') as r_w:
25             lines1=r_w.readlines()
26             with open('student_infor.txt','w') as rr_w:
27                 for line1 in lines1:
28                     if num3 in line1:
29                         rr_w.write(num3+' '+name3+' '+score3+'\n')
30                         continue
31                     rr_w.write(line1)
modify code

通过学生成绩高低查询学生信息:

              

 1 #查询学生成绩
 2 class PageFour(tk.Frame):
 3     def __init__(self, parent, root):
 4         super().__init__(parent)
 5         label = tk.Label(self, text="查询学生成绩", font=LARGE_FONT)
 6         label.pack(pady=100)
 7         tree_data=ttk.Treeview()
 8         ft4=tkFont.Font(size=12)
 9        #滚动条
10 
11         scro=Scrollbar(self)
12 
13         scro.pack(side=RIGHT,fill=Y)
14         lista=Listbox(self,yscrollcommand=scro.set,width=50)
15 
16         f=open('student_infor.txt','r')
17         text=("                 %-16s%-16s%-16s"%("学号","姓名","成绩"))
18 
19         li=[]
20         for i in f.readlines():
21             j=i.split(' ')
22             j[2]=j[2].replace('\n','')
23             li.append(j)
24             li.sort(key=lambda x:x[2],reverse=False)
25         for i in li:
26             text1=("                %-16s%-16s%-16s"%(i[0],i[1],i[2]))
27             lista.insert(0,text1)
28         f.close()
29         lista.insert(0,text)
30         lista.pack()
31         Button(self, text="返回首页",width=8,font=ft4,command=lambda: root.show_frame(StartPage)).pack(pady=40)
View Search Code

最后,必不可少的

1 if __name__ == '__main__':
2     # 实例化Application
3     app = Application()
4     
5     # 主消息循环:
6     app.mainloop()

没了,新手一枚,请多多指教

。。。。。。

原文地址:https://www.cnblogs.com/xy-ju24/p/9212855.html