【基礎(chǔ)】練習(xí)冊(cè)42-Python3_帶數(shù)據(jù)庫(kù)的學(xué)生管理系統(tǒng)

界面為:


代碼如下:

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末获列,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子蛔垢,更是在濱河造成了極大的恐慌击孩,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鹏漆,死亡現(xiàn)場(chǎng)離奇詭異巩梢,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)艺玲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門括蝠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人饭聚,你說(shuō)我怎么就攤上這事忌警。” “怎么了秒梳?”我有些...
    開封第一講書人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵法绵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我酪碘,道長(zhǎng)朋譬,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任婆跑,我火速辦了婚禮此熬,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘滑进。我一直安慰自己,他們只是感情好募谎,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開白布扶关。 她就那樣靜靜地躺著,像睡著了一般数冬。 火紅的嫁衣襯著肌膚如雪节槐。 梳的紋絲不亂的頭發(fā)上搀庶,一...
    開封第一講書人閱讀 52,457評(píng)論 1 311
  • 那天,我揣著相機(jī)與錄音铜异,去河邊找鬼哥倔。 笑死,一個(gè)胖子當(dāng)著我的面吹牛揍庄,可吹牛的內(nèi)容都是我干的咆蒿。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼蚂子,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼沃测!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起食茎,我...
    開封第一講書人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蒂破,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后别渔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體附迷,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年哎媚,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了挟秤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡抄伍,死狀恐怖艘刚,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情截珍,我是刑警寧澤攀甚,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站岗喉,受9級(jí)特大地震影響秋度,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜钱床,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一荚斯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧查牌,春花似錦事期、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至胁孙,卻和暖如春唠倦,著一層夾襖步出監(jiān)牢的瞬間称鳞,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工稠鼻, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留冈止,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓候齿,卻偏偏與公主長(zhǎng)得像熙暴,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子毛肋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

推薦閱讀更多精彩內(nèi)容