1. 程式人生 > >python tkinter 學生信息管理系統

python tkinter 學生信息管理系統

學生 span ace 滾動條 exp with open button 管理 nta

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

下面貼出的是主界面和添加學生信息界面,剩下的就不貼出來了。。。。。。。。。

技術分享圖片 技術分享圖片

以下就是完整的代碼:

  1 from tkinter import *
  2 import tkinter.font as tkFont
  3 import tkinter as tk
  4 from tkinter import ttk
  5 
  6 LARGE_FONT= ("Verdana", 20)
  7 
  8 #窗口每頁內容更換
  9 class Application(tk.Tk):
10 def __init__(self): 11 12 super().__init__() 13 14 self.wm_title("學生信息管理系統") 15 16 container = tk.Frame(self) 17 container.pack(side="top", fill="both", expand = True) 18 container.grid_rowconfigure(0, weight=1) 19 container.grid_columnconfigure(0, weight=1)
20 21 self.frames = {} 22 for F in (StartPage, PageOne, PageTwo, PageThree,PageFour): 23 frame = F(container, self) 24 self.frames[F] = frame 25 frame.grid(row=0, column=0, sticky="nsew") # 四個頁面的位置都是 grid(row=0, column=0), 位置重疊,只有最上面的可見!! 26 27
self.show_frame(StartPage) 28 29 30 def show_frame(self, cont): 31 frame = self.frames[cont] 32 frame.tkraise() # 切換,提升當前 tk.Frame z軸順序(使可見)!!此語句是本程序的點睛之處 33 34 35 class StartPage(tk.Frame): 36 ‘‘‘主頁‘‘‘ 37 def __init__(self, parent, root): 38 super().__init__(parent) 39 label = tk.Label(self, text="學生信息管理系統", font=LARGE_FONT) 40 label.pack(pady=100) 41 ft2=tkFont.Font(size=16) 42 Button(self, text="添加學生信息",font=ft2,command=lambda: root.show_frame(PageOne),width=30,height=2,fg=white,bg=gray,activebackground=black,activeforeground=white).pack() 43 Button(self, text="刪除學生信息",font=ft2,command=lambda: root.show_frame(PageTwo),width=30,height=2).pack() 44 Button(self, text="修改學生信息",font=ft2,command=lambda: root.show_frame(PageThree),width=30,height=2,fg=white,bg=gray,activebackground=black,activeforeground=white).pack() 45 Button(self, text="查詢學生信息",font=ft2,command=lambda: root.show_frame(PageFour),width=30,height=2).pack() 46 Button(self,text=退出系統,height=2,font=ft2,width=30,command=root.destroy,fg=white,bg=gray,activebackground=black,activeforeground=white).pack() 47 48 49 50 #添加學生信息 51 class PageOne(tk.Frame): 52 def __init__(self, parent, root): 53 super().__init__(parent) 54 label = tk.Label(self, text="添加學生信息", font=LARGE_FONT) 55 label.pack(pady=100) 56 57 ft3=tkFont.Font(size=14) 58 ft4=tkFont.Font(size=12) 59 Label(self,text=學生學號:,font=ft3).pack(side=TOP) 60 global e1 61 e1=StringVar() 62 Entry(self,width=30,textvariable=e1,font=ft3,bg=Ivory).pack(side=TOP) 63 Label(self,text=學生姓名:,font=ft3).pack(side=TOP) 64 global e2 65 e2=StringVar() 66 Entry(self,width=30,textvariable=e2,font=ft3,bg=Ivory).pack(side=TOP) 67 Label(self,text=學生成績:,font=ft3).pack(side=TOP) 68 global e3 69 e3=StringVar() 70 Entry(self,width=30,textvariable=e3,font=ft3,bg=Ivory).pack(side=TOP) 71 Button(self, text="返回首頁",width=8,font=ft4,command=lambda: root.show_frame(StartPage)).pack(pady=20) 72 Button(self, text="確定保存",width=8,font=ft4,command=self.save).pack(side=TOP) 73 74 def save(self): 75 with open(student_infor.txt,a+) as student_infor: 76 num=str(e1.get()) 77 name=str(e2.get()) 78 score=str(e3.get()) 79 student_infor.write(num+ +name+ +score+\n) 80 81 #刪除學生信息 82 class PageTwo(tk.Frame): 83 def __init__(self, parent, root): 84 super().__init__(parent) 85 label = tk.Label(self, text="添加學生信息", font=LARGE_FONT) 86 label.pack(pady=100) 87 88 ft3=tkFont.Font(size=14) 89 ft4=tkFont.Font(size=12) 90 Label(self,text=請輸入你要刪除的學生學號:,font=ft3).pack(side=TOP) 91 global e4 92 e4=StringVar() 93 Entry(self,width=30,textvariable=e4,font=ft3,bg=Ivory).pack(side=TOP) 94 Button(self, text="確定刪除",width=8,font=ft4,command=self.del1).pack(pady=20) 95 Button(self, text="返回首頁",width=8,font=ft4,command=lambda: root.show_frame(StartPage)).pack() 96 def del1(self): 97 num2=str(e4.get()) 98 with open(student_infor.txt,r) as f: 99 lines=f.readlines() 100 with open(student_infor.txt,w) as f_w: 101 for line in lines: 102 if num2 in line: 103 continue 104 f_w.write(line) 105 106 #修改學生信息 107 class PageThree(tk.Frame): 108 def __init__(self, parent, root): 109 super().__init__(parent) 110 tk.Label(self, text="修改學生信息", font=LARGE_FONT).pack(pady=100) 111 112 ft3=tkFont.Font(size=14) 113 ft4=tkFont.Font(size=12) 114 Label(self,text=請輸入你要修改的學生學號:,font=ft3).pack(side=TOP) 115 self.e5=StringVar() 116 Entry(self,width=30,textvariable=self.e5,font=ft3,bg=Ivory).pack(side=TOP) 117 Label(self,text=學生姓名:,font=ft3).pack(side=TOP) 118 self.e6=StringVar() 119 Entry(self,width=30,textvariable=self.e6,font=ft3,bg=Ivory).pack(side=TOP) 120 Label(self,text=學生成績:,font=ft3).pack(side=TOP) 121 self.e7=StringVar() 122 Entry(self,width=30,textvariable=self.e7,font=ft3,bg=Ivory).pack(side=TOP) 123 Button(self, text="確定修改",width=8,font=ft4,command=self.modify).pack(pady=20) 124 Button(self, text="返回首頁",width=8,font=ft4,command=lambda: root.show_frame(StartPage)).pack() 125 def modify(self): 126 num3=str(self.e5.get()) 127 name3=str(self.e6.get()) 128 score3=str(self.e7.get()) 129 with open(student_infor.txt,r) as r_w: 130 lines1=r_w.readlines() 131 with open(student_infor.txt,w) as rr_w: 132 for line1 in lines1: 133 if num3 in line1: 134 rr_w.write(num3+ +name3+ +score3+\n) 135 continue 136 rr_w.write(line1) 137 138 #查詢學生成績 139 class PageFour(tk.Frame): 140 def __init__(self, parent, root): 141 super().__init__(parent) 142 label = tk.Label(self, text="查詢學生成績", font=LARGE_FONT) 143 label.pack(pady=100) 144 tree_data=ttk.Treeview() 145 ft4=tkFont.Font(size=12) 146 #滾動條 147 148 scro=Scrollbar(self) 149 150 scro.pack(side=RIGHT,fill=Y) 151 lista=Listbox(self,yscrollcommand=scro.set,width=50) 152 153 f=open(student_infor.txt,r) 154 text=(" %-16s%-16s%-16s"%("學號","姓名","成績")) 155 156 li=[] 157 for i in f.readlines(): 158 j=i.split( ) 159 j[2]=j[2].replace(\n,‘‘) 160 li.append(j) 161 li.sort(key=lambda x:x[2],reverse=False) 162 for i in li: 163 text1=(" %-16s%-16s%-16s"%(i[0],i[1],i[2])) 164 lista.insert(0,text1) 165 f.close() 166 lista.insert(0,text) 167 lista.pack() 168 Button(self, text="返回首頁",width=8,font=ft4,command=lambda: root.show_frame(StartPage)).pack(pady=40) 169 170 171 if __name__ == __main__: 172 # 實例化Application 173 app = Application() 174 175 # 主消息循環: 176 app.mainloop()

2018-06-22

python tkinter 學生信息管理系統