界面為:
代碼如下:
# 學(xué)生信息管理系統(tǒng):顯示所有信息专执、追加信息、刪除信息理茎、查詢
# 細(xì)節(jié):清空控件內(nèi)容
# 待增加功能:查詢?cè)黾邮髽?biāo)事件來(lái)進(jìn)行修改
# Treeview
import tkinter as tk
import tkinter.messagebox
import string
import database
from tkinter import ttk
win = tk.Tk()
win.geometry('500x400+200+200')
win.title('學(xué)生管理系統(tǒng)')
#win.grid_rowconfigure(win.grid_size(), minsize=1)
# 界面標(biāo)題
tk.Label(text='學(xué)生管理系統(tǒng)',
? ? ? ? font=('宋體', 16, 'bold'),
? ? ? ? fg='blue',
? ? ? ? justify=tk.CENTER,
? ? ? ? width=36).grid(row=1, column=1, columnspan=6, pady=10)
# 界面設(shè)置
label1 = tk.Label(text='學(xué)號(hào):', justify=tk.RIGHT, width=4).grid(row=2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? column=1,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pady=5)
label2 = tk.Label(text='姓名:', justify=tk.RIGHT, width=4).grid(row=2, column=4)
label3 = tk.Label(text='電話:', justify=tk.RIGHT, width=4).grid(row=3,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? column=1,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pady=5)
label4 = tk.Label(text='地址:', justify=tk.RIGHT, width=4).grid(row=3, column=4)
stu_id = tk.IntVar().set(None)
stu_na = tk.StringVar()
stu_ph = tk.IntVar().set(None)
stu_adr = tk.StringVar()
entryid = tk.Entry(win, textvariable=stu_id, width=18)
entryid.grid(row=2, column=2, columnspan=2, sticky='w')
entryna = tk.Entry(win, textvariable=stu_na, width=18)
entryna.grid(row=2, column=5, columnspan=2, sticky='w')
entryph = tk.Entry(win, textvariable=stu_ph, width=18)
entryph.grid(row=3, column=2, columnspan=2, sticky='w')
entryadr = tk.Entry(win, textvariable=stu_adr, width=18)
entryadr.grid(row=3, column=5, columnspan=2, sticky='w')
tree = ttk.Treeview(win, show='headings', columns=('學(xué)號(hào)', '姓名', '電話', '地址'))
# 定義列
tree.column('學(xué)號(hào)', width=100)
tree.column('姓名', width=100)
tree.column('電話', width=100)
tree.column('地址', width=100)
# 定義頭
tree.heading('學(xué)號(hào)', text='學(xué)號(hào)')
tree.heading('姓名', text='姓名')
tree.heading('電話', text='電話')
tree.heading('地址', text='地址')
def func(event):
? ? pass
tree.bind("<Button-3>", func)? # <Button-3>鼠標(biāo)右鍵
tree.grid(row=6, column=1, columnspan=6, pady=5, sticky='e')
def Judge(stuid):? # 判斷stuid(str型)是否存在庫(kù)中
? ? flag = 0? # 判斷標(biāo)志绑洛,1為stuid存在數(shù)據(jù)庫(kù)中
? ? lst = database.showAllData()
? ? for i in range(len(lst)):
? ? ? ? for item in lst[i]:
? ? ? ? ? ? if int(stuid) == item:
? ? ? ? ? ? ? ? flag = 1
? ? return flag
def ClearEntry():? # 清空控件內(nèi)容
? ? entryid.delete(0, 'end')
? ? entryna.delete(0, 'end')
? ? entryph.delete(0, 'end')
? ? entryadr.delete(0, 'end')
def ClearTree():? # 清空樹控件
? ? x = tree.get_children()
? ? for item in x:? # 清空控件內(nèi)容
? ? ? ? tree.delete(item)
def addInformation():
? ? res = [entryid.get(), entryna.get(), entryph.get(), entryadr.get()]
? ? if (not entryid.get()) or (not entryna.get()) or (not entryph.get()) or (
? ? ? ? ? ? not entryadr.get()):
? ? ? ? tk.messagebox.showerror(title='Attention', message='輸入信息不完整')
? ? elif (len(entryid.get()) != 8) or not (entryid.get()).isdecimal():
? ? ? ? tk.messagebox.showerror(title='Attention', message='輸入錯(cuò)誤,學(xué)號(hào)為8位數(shù)字')
? ? elif len(entryph.get()) != 11 or not (entryph.get()).isdecimal():
? ? ? ? tk.messagebox.showerror(title='Attention', message='輸入錯(cuò)誤泵琳,電話為11位數(shù)字')
? ? else:
? ? ? ? flag = Judge(entryid.get())? # 判斷id是否已存在
? ? ? ? if not flag:
? ? ? ? ? ? database.addRec(int(entryid.get()), entryna.get(),
? ? ? ? ? ? ? ? ? ? ? ? ? ? int(entryph.get()), entryadr.get())
? ? ? ? ? ? tk.messagebox.showinfo(title='Attention', message='添加成功')
? ? ? ? ? ? tree.insert('', 'end', values=res)
? ? ? ? ? ? ClearEntry()? # 清空所有輸入框
? ? ? ? else:
? ? ? ? ? ? tk.messagebox.showinfo(title='Attention', message='學(xué)號(hào)已存在')
def deleteInformation():? # 刪除
? ? temp = entryid.get()
? ? if not temp:
? ? ? ? tk.messagebox.showinfo(title='Attention', message='學(xué)生學(xué)號(hào)為空')
? ? elif not temp.isdecimal() or (len(entryid.get()) != 8):
? ? ? ? tk.messagebox.showerror(title='Attention', message='輸入錯(cuò)誤,學(xué)號(hào)為8位數(shù)字')
? ? else:
? ? ? ? flag = Judge(temp)
? ? ? ? if flag:? # 存在
? ? ? ? ? ? database.deleteRec(temp)
? ? ? ? ? ? tk.messagebox.showinfo(title='Attention', message='刪除成功')
? ? ? ? ? ? showData()? # 刷新顯示數(shù)據(jù)
? ? ? ? ? ? Clear()? # 清空所有輸入框
? ? ? ? else:
? ? ? ? ? ? tk.messagebox.showinfo(title='Attention', message='學(xué)號(hào)不存在')
def showInformation():? # 顯示全部信息
? ? ClearTree()
? ? lst = database.showAllData()
? ? for item in lst:
? ? ? ? tree.insert('', 'end', values=item)
def searchInformation():
? ? temp = entryid.get()
? ? if not temp:
? ? ? ? tk.messagebox.showinfo(title='Attention', message='學(xué)生學(xué)號(hào)為空')
? ? elif not temp.isdecimal() or (len(entryid.get()) != 8):
? ? ? ? tk.messagebox.showerror(title='Attention', message='輸入錯(cuò)誤誊役,學(xué)號(hào)為8位數(shù)字')
? ? else:
? ? ? ? flag = Judge(temp)
? ? ? ? ClearTree()
? ? ? ? if flag:? # 存在
? ? ? ? ? ? res = database.searchRec(temp)
? ? ? ? ? ? tree.insert('', 'end', values=res)
? ? ? ? else:
? ? ? ? ? ? tk.messagebox.showinfo(title='Attention', message='學(xué)號(hào)不存在')
bt1 = tk.Button(win, text='顯示所有信息', width=10,
? ? ? ? ? ? ? ? command=showInformation).grid(row=4, column=2, pady=10)
bt2 = tk.Button(win, text='追加信息', width=10,
? ? ? ? ? ? ? ? command=addInformation).grid(row=4, column=3, pady=10)
bt3 = tk.Button(win, text='刪除信息', width=10,
? ? ? ? ? ? ? ? command=deleteInformation).grid(row=4, column=4, pady=10)
bt4 = tk.Button(win, text='搜索信息', width=10,
? ? ? ? ? ? ? ? command=searchInformation).grid(row=4, column=5, pady=10)
win.mainloop()