python tkinter 學生信息管理系統

使用tkinter模塊,python3.6,主要功能有添加,查詢,刪除,修改學生信息python

使用模版:app

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軸順序(使可見)!!此語句是本程序的點睛之處

 

下面貼出的是主界面,ide

                                       

這是主頁面部分代碼:oop

 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()

添加學生信息頁面:spa

             

第二功能頁面添加學生信息,,code

 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')

刪除學生信息blog

              

 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)

修改學生信息:get

                

 

 

 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

經過學生成績高低查詢學生信息:it

              

 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()

沒了,新手一枚,請多多指教

。。。。。。

相關文章
相關標籤/搜索