讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python實(shí)現(xiàn)一個(gè)完整版學(xué)生成績(jī)管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平 學(xué)生成績(jī)管理系統(tǒng)簡(jiǎn)介一個(gè)帶有登錄界面具有增減改查功能的學(xué)生成績(jī)管理系統(tǒng)(面向?qū)ο笏枷耄胻kinter庫(kù)進(jìn)行制作,利用.txt文件進(jìn)行存儲(chǔ)數(shù)據(jù)) 源代碼?僅供學(xué)習(xí)參考,最好還是自己多敲多練習(xí)(實(shí)踐是檢驗(yàn)真理的唯一標(biāo)準(zhǔn)) students.txt用于存儲(chǔ)數(shù)據(jù) main.pyfrom tkinter import *from Login import *import tkinter as tkroot = tk.Tk()root.title('歡迎進(jìn)入學(xué)生成績(jī)管理系統(tǒng)')LoginPage(root)root.mainloop() Login.pyfrom tkinter import *from tkinter.messagebox import *from MenuPage import *class LoginPage(object): def __init__(self, master=None): self.root = master # 定義內(nèi)部變量root self.root.geometry('%dx%d' % (300, 180)) # 設(shè)置窗口大小 self.username = StringVar() self.password = StringVar() self.createPage() def createPage(self): self.page = Frame(self.root) # 創(chuàng)建Frame self.page.pack() Label(self.page).grid(row=0, stick=W) Label(self.page, text='賬戶: ').grid(row=1, stick=W, pady=10) Entry(self.page, textvariable=self.username).grid(row=1, column=1, stick=E) Label(self.page, text='密碼: ').grid(row=2, stick=W, pady=10) Entry(self.page, textvariable=self.password, show='*').grid(row=2, column=1, stick=E) Button(self.page, text='登陸', command=self.loginCheck).grid(row=3, stick=W, pady=10) Button(self.page, text='退出', command=self.page.quit).grid(row=3, column=1, stick=E) def loginCheck(self): name = self.username.get() password = self.password.get() if name == 'hacker707' and password == 'admin': self.page.destroy() MenuPage(self.root) else: showinfo(title='錯(cuò)誤', message='賬號(hào)或密碼錯(cuò)誤!') db.pyimport jsonclass StudentDB(object): def __init__(self): self.students = [] self._load_students_data() def insert(self, student): self.students.append(student) print(self.students) def all(self): return self.students def delete_by_name(self, name): # 刪除數(shù)據(jù) for student in self.students: if name == student['name']: self.students.remove(student) break else: return False return True # 查詢 def search_by_name(self, name): for student in self.students: if name == student['name']: return student # 姓名+成績(jī) else: return False # 修改 def update(self, stu): # 修改數(shù)據(jù) name = stu['name'] for student in self.students: if name == student['name']: student.update(stu) return True else: return False # 加載文件 def _load_students_data(self): with open('students.txt', 'r', encoding='utf-8') as f: text = f.read() if text: self.students = json.loads(text) # 保存數(shù)據(jù) def save_data(self): with open('students.txt', 'w', encoding='utf-8') as f: text = json.dumps(self.students, ensure_ascii=False) f.write(text)db = StudentDB() MenuPage.pyimport tkinter as tkfrom view import *class MenuPage(object): def __init__(self, master=None): self.root = master self.root.geometry('%dx%d' % (600, 400)) self.create_page() self.input_page = InputFrame(self.root) self.query_page = QuerryFrame(self.root) self.delete_page = DeleteFrame(self.root) self.update_page = UpdateFrame(self.root) self.about_page = AboutFrame(self.root) self.input_page.pack() def create_page(self): # 創(chuàng)建菜單對(duì)象 menubar = tk.Menu(self.root) # add_command 添加 menubar.add_command(label='錄入', command=self.input_data) # label menubar.add_command(label='查詢', command=self.query_data) # label menubar.add_command(label='刪除', command=self.delete_data) # label menubar.add_command(label='修改', command=self.update_data) # label menubar.add_command(label='關(guān)于', command=self.about_data) # label # 設(shè)置菜單欄 self.root.config(menu=menubar) # 切換界面 def input_data(self): self.input_page.pack() self.update_page.pack_forget() self.delete_page.pack_forget() self.about_page.pack_forget() self.query_page.pack_forget() def query_data(self): self.input_page.pack_forget() self.query_page.pack() self.update_page.pack_forget() self.delete_page.pack_forget() self.about_page.pack_forget() def update_data(self): self.input_page.pack_forget() self.update_page.pack() self.delete_page.pack_forget() self.about_page.pack_forget() self.query_page.pack_forget() def delete_data(self): self.input_page.pack_forget() self.update_page.pack_forget() self.delete_page.pack() self.about_page.pack_forget() self.query_page.pack_forget() def about_data(self): self.input_page.pack_forget() self.update_page.pack_forget() self.delete_page.pack_forget() self.about_page.pack() self.query_page.pack_forget() view.pyimport tkinter as tkfrom db import dbfrom tkinter import ttk# 錄入類class InputFrame(tk.Frame): def __init__(self, master=None): super().__init__(master) self.root = master self.name = tk.StringVar() self.math = tk.StringVar() self.chinese = tk.StringVar() self.english = tk.StringVar() self.status = tk.StringVar() self.create_page() def create_page(self): tk.Label(self).grid(row=0, stick=tk.W, pady=10) tk.Label(self, text='姓名:').grid(row=1, stick=tk.W, pady=10) # 單行文本框 entry,textvariable綁定變量 tk.Entry(self, textvariable=self.name).grid(row=1, column=1, stick=tk.E) tk.Label(self, text='數(shù)學(xué):').grid(row=2, stick=tk.W, pady=10) # 單行文本框 entry,textvariable綁定變量 tk.Entry(self, textvariable=self.math).grid(row=2, column=1, stick=tk.E) tk.Label(self, text='語(yǔ)文:').grid(row=3, stick=tk.W, pady=10) # 單行文本框 entry,textvariable綁定變量 tk.Entry(self, textvariable=self.chinese).grid(row=3, column=1, stick=tk.E) tk.Label(self, text='英語(yǔ):').grid(row=4, stick=tk.W, pady=10) # 單行文本框 entry,textvariable綁定變量 tk.Entry(self, textvariable=self.english).grid(row=4, column=1, stick=tk.E) tk.Button(self, text='錄入', command=self.recode_student).grid(row=5, column=1, stick=tk.E, pady=10) tk.Label(self, textvariable=self.status).grid(row=6, column=1, stick=tk.E, pady=10) # 錄入成績(jī) def recode_student(self): student = { 'name': self.name.get(), 'math': self.math.get(), 'chinese': self.chinese.get(), 'english': self.english.get(), } # 一個(gè)學(xué)生的成績(jī) db.insert(student) # get()得到值 # set()設(shè)置值 self.status.set('插入數(shù)據(jù)成功!') self._clear_data() db.save_data() # 清空文本數(shù)據(jù) def _clear_data(self): self.name.set('') self.math.set('') self.chinese.set('') self.english.set('')# 查詢類class QuerryFrame(tk.Frame): def __init__(self, master=None): super().__init__(master) self.root = master self.create_page() # 創(chuàng)建查詢界面 def create_page(self): self.create_tree_view() self.show_data_frame() # grid() tk.Button(self, text='刷新數(shù)據(jù)', command=self.show_data_frame).pack(anchor=tk.E, pady=5) # Treeview def create_tree_view(self): # 表頭 columns = ('name', 'chinese', 'math', 'english') self.tree_view = ttk.Treeview(self, show='headings', columns=columns) self.tree_view.column('name', width=80, anchor='center') self.tree_view.column('chinese', width=80, anchor='center') self.tree_view.column('math', width=80, anchor='center') self.tree_view.column('english', width=80, anchor='center') self.tree_view.heading('name', text='姓名') self.tree_view.heading('chinese', text='語(yǔ)文') self.tree_view.heading('math', text='數(shù)學(xué)') self.tree_view.heading('english', text='英語(yǔ)') self.tree_view.pack() # 顯示數(shù)據(jù) def show_data_frame(self): # 刪除原節(jié)點(diǎn) map(int,值) for i in map(self.tree_view.delete, self.tree_view.get_children('')): pass # 拿到列表里面所有值、students[] students = db.all() # 同時(shí)拿到索引跟value值 for index, stu in enumerate(students): self.tree_view.insert('', index, values=(stu['name'], stu['chinese'], stu ['math'], stu['english']))class DeleteFrame(tk.Frame): def __init__(self, master=None): super().__init__(master) tk.Label(self, text='刪除數(shù)據(jù)').pack() self.status = tk.StringVar() self.de_name = tk.StringVar() # 獲取刪除學(xué)生的姓名 self.create_page() # 創(chuàng)建界面 def create_page(self): tk.Label(self, text='根據(jù)姓名刪除信息').pack(anchor=tk.W, padx=20) e1 = tk.Entry(self, textvariable=self.de_name) e1.pack(side=tk.LEFT, padx=20, pady=5) tk.Button(self, text='刪除', command=self._delete).pack(side=tk.RIGHT) tk.Label(self, textvariable=self.status).pack() # 刪除 def _delete(self): name = self.de_name.get() print(name) result = db.delete_by_name(name) if result: self.status.set(f'{name}已經(jīng)被刪') self.de_name.set('') else: self.status.set(f'{name}不存在')class UpdateFrame(tk.Frame): def __init__(self, master=None): super().__init__(master) self.root = master tk.Label(self, text='修改界面').pack() self.change_frame = tk.Frame(self) self.change_frame.pack() self.name = tk.StringVar() self.math = tk.StringVar() self.chinese = tk.StringVar() self.english = tk.StringVar() self.status = tk.StringVar() self.create_page() def create_page(self): tk.Label(self.change_frame).grid(row=0, stick=tk.W, pady=10) tk.Label(self.change_frame, text='姓名:').grid(row=1, stick=tk.W, pady=10) # 單行文本框 entry,textvariable綁定變量 tk.Entry(self.change_frame, textvariable=self.name).grid(row=1, column=1, stick=tk.E) tk.Label(self.change_frame, text='數(shù)學(xué):').grid(row=2, stick=tk.W, pady=10) # 單行文本框 entry,textvariable綁定變量 tk.Entry(self.change_frame, textvariable=self.math).grid(row=2, column=1, stick=tk.E) tk.Label(self.change_frame, text='語(yǔ)文:').grid(row=3, stick=tk.W, pady=10) # 單行文本框 entry,textvariable綁定變量 tk.Entry(self.change_frame, textvariable=self.chinese).grid(row=3, column=1, stick=tk.E) tk.Label(self.change_frame, text='英語(yǔ):').grid(row=4, stick=tk.W, pady=10) # 單行文本框 entry,textvariable綁定變量 tk.Entry(self.change_frame, textvariable=self.english).grid(row=4, column=1, stick=tk.E) # 按鈕 tk.Button(self.change_frame, text='查詢', command=self._search).grid(row=6, column=0, stick=tk.W, pady=10) tk.Button(self.change_frame, text='修改', command=self._change).grid(row=6, column=1, stick=tk.E, pady=10) tk.Label(self.change_frame, textvariable=self.status).grid(row=7, column=1, stick=tk.E, pady=10) # 查詢 def _search(self): name = self.name.get() student = db.search_by_name(name) if student: self.math.set(student['math']) self.chinese.set(student['chinese']) self.english.set(student['english']) self.status.set(f'查詢到{name}同學(xué)的信息') else: self.status.set(f'沒(méi)有查詢到{name}同學(xué)的信息') # 更改成績(jī) def _change(self): name = self.name.get() math = self.math.get() chinese = self.chinese.get() english = self.english.get() stu = { 'name': name, 'math': math, 'chinese': chinese, 'english': english, } r = db.update(stu) if r: self.status.set(f'{name}同學(xué)的信息更新完畢') else: self.status.set(f'{name}同學(xué)的信息更新失敗')class AboutFrame(tk.Frame): def __init__(self, master=None): super().__init__(master) self.root = master self.create_page() def create_page(self): tk.Label(self, text='關(guān)于本作品(人生苦短,我用python)').pack(anchor=tk.W) 原文鏈接: |
|