一,功能簡介
除最基本的增,刪,改,查,多加了一個匯出為excel表格的功能,比較適合剛學完python練習基本語言用,代碼中大部分都是使用基本語法來處理資料,

新建和查看


搜索時,界面確實不怎么美觀,家庭住址這一欄也直接到了下一行,時間原因,也懶得修改了,

修改:
先選擇要修改的資訊

改動:

資訊覆寫:

洗掉和修改的界面幾乎一樣一個多選的對話框,這里不展示了,
匯出表格:其中C:/Users/acer/Desktop/資訊.xlsx為默認路徑,選擇匯出功能時會自動填入


二,代碼
個人小白,python學的比較淺,GUI也是用的自帶模塊easygui
運行時會生成名為employee_date.json的檔案來存盤資訊
main.py:
import easygui
import function as fun
while True:
main_menu = easygui.buttonbox('\t\t\t\t選單界面', title='資訊管理系統',
choices=['新建資訊', '查看所有資訊',
'搜索資訊', '修改資訊', '洗掉資訊',
'匯出為excel表格'])
if main_menu == '新建資訊':
try:
fun.new_employee()
except FileNotFoundError:
fun.new_filename()
elif main_menu == '查看所有資訊':
try:
fun.data_all()
except FileNotFoundError:
fun.tip_not_found_file()
elif main_menu == '修改資訊':
try:
fun.modify_employee()
except FileNotFoundError:
fun.tip_not_found_file()
elif main_menu == '洗掉資訊':
try:
fun.del_data()
except ValueError:
fun.del_data2()
except FileNotFoundError:
fun.tip_not_found_file()
elif main_menu == '搜索資訊':
try:
fun.search_data()
except FileNotFoundError:
fun.tip_not_found_file()
elif main_menu == '匯出為excel表格':
try:
fun.export_to_excel()
except FileNotFoundError:
fun.tip_not_found_file()
elif main_menu == None:
break
function.py:
import json
import openpyxl
import easygui
import os
list_employee = []
filename = 'employee_date.json'
def new_filename():
employee = new_tools1()
if employee == None:
return
tem_list = []
tem_list.append(employee)
with open(filename, 'w') as f:
json.dump(tem_list, f, indent=4)
f.close()
easygui.msgbox(msg='Add success', title='tip', ok_button='回傳')
def new_employee():
with open(filename) as f:
list_employee = json.load(f)
f.close()
employee = new_tools1()
if employee == None:
return
with open(filename) as f:
list_employee = json.load(f)
list_employee.append(employee)
f.close()
with open(filename, 'w') as f:
json.dump(list_employee, f, indent=4)
f.close()
easygui.msgbox(msg='Add success', title='tip', ok_button='回傳')
def new_tools1():
menu1 = easygui.multenterbox('新建資訊', '新建資訊功能',
['姓名', '性別(男/女)', '聯系方式', '家庭住址'])
if menu1 == None:
return menu1
while len(menu1[1]) != 1 or len(menu1[2]) != 11 or not menu1[2].isdigit():
menu1 = easygui.multenterbox('格式有誤!重新輸入', '新建資訊功能',
['姓名', '性別(男/女)', '聯系方式', '家庭住址'])
if menu1 == None:
return menu1
employee = {
'name': menu1[0],
'gender': menu1[1],
'phone': menu1[2],
'address': menu1[3]
}
return employee
def data_all(w=True):
with open(filename) as f:
data = json.load(f)
f.close()
if len(data) == 0:
easygui.msgbox('NOT FOUND MESSAGES!')
return 1
value = ['姓名', '性別', '聯系方式', '家庭住址']
i = 0
while i < len(data):
for v in data[i].values():
value.append(v)
i = i+1
val_p = ''
for a in range(len(value)):
val_p += str(value[a])+'\t\t'
if a % 4 == 3:
val_p += '\n'
if w == False:
return val_p
easygui.msgbox(val_p, title='全部資訊')
def export_to_excel():
# 定義要寫入的行和列的值
with open(filename) as f:
list_employee = json.load(f)
# 定義excel的sheet_name "xlsx格式測驗表 "
f.close()
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet_name = "xlsx格式表"
sheet.title = sheet_name
path = 'C:/Users/acer/Desktop/資訊.xlsx'
path = easygui.enterbox('匯出路徑', title='選擇路徑', default=path)
if path == None:
return
temporary_val_k = ['姓名', '性別', '聯系方式', '家庭住址']
for a in range(0, len(temporary_val_k)):
sheet.cell(row=1, column=a+1, value=temporary_val_k[a]) #列印表頭
i = 0
while i < len(list_employee): #0~len(list) i:0~4
temporary_val = []
for val in list_employee[i].values(): #list_employee is a list ,每個值為字典
temporary_val.append(val) #此時temporary_val 是串列,有四個值
for j in range(len(temporary_val)):
sheet.cell(i+2, j+1, temporary_val[j])
i = i+1
workbook.save(path)
easygui.msgbox(msg='匯出成功', title='tip')
def search_data():
val_p = data_all(w=False)
if val_p == 1:
return
find_data = easygui.enterbox(val_p,
title='資訊搜索')
if find_data == None:
return
with open(filename) as f:
list_employee = json.load(f)
f.close()
value = ['姓名', '性別', '聯系方式', '家庭住址']
i = len(value)
s = ''
for category in ['name', 'gender', 'phone', 'address']:
for dict_employee in list_employee:
if find_data in dict_employee[category]:
for element in dict_employee.values():
value.append(element)
if len(value) == 4:
value.append('NOT FOUND!!')
for a in range(len(value)):
s += value[a]+'\t\t'
if a%i == 3:
s += '\n'
easygui.msgbox(msg=s, title='資訊檢索', ok_button='回傳')
def del_data():
tem = del_mod(m='選擇要洗掉的資訊', t='選擇洗掉資訊')
if tem == None:
return
for t in range(len(tem)):
num = ''.join([x for x in tem[t] if x.isdigit()])
with open(filename) as f:
list_employee = json.load(f)
for employee in list_employee:
if employee['phone'] == num:
list_employee.remove(employee)
f.close()
with open(filename, 'w') as f:
json.dump(list_employee, f, indent=4)
f.close()
message = '成功洗掉:\n'
for a in range(len(tem)):
message += tem[a]+'\n'
easygui.msgbox(message, title='tip')
def del_data2(): #解決只有一個資訊時的洗掉錯誤
with open(filename) as f:
list_employee = json.load(f)
f.close()
a = '成功洗掉:\n'
for a_p in list_employee[0].values():
a += a_p
os.remove(filename)
easygui.msgbox(a, title='tip')
def modify_employee():
with open(filename) as f:
list_employee = json.load(f)
f.close()
list_tem = []
value = ['姓名', '性別', '聯系方式', '家庭住址']
tem = del_mod(m='選擇要修改的資訊', t='修改資訊') #tem is a list
if tem == None:
return
for d in tem:
for n in range(len(d)):
t = d[n]
if t == '男' or t == '女': #別問為什么要用中間變數t,直接用d[n]不是更簡單嗎?直接用d[n]不行,不是預期結果
list_tem.append(d[:n].strip())
list_tem.append(d[n])
list_tem.append(d[n+3:n+14])
list_tem.append(d[n+14:].strip())
num_list = get_subscript(list_tem) #若選擇要修改的人員資訊有兩個,此時list_tem == 8,num_list存盤要修改資訊的下標
list_tem_a = []
record = 0
for a in range(len(list_tem)): #run twice
list_tem_a.append(list_tem[a]) #給list_tem_a添加list_tem,當加入四個時說明為一個人的資訊
if a % len(value) == len(value)-1: #if a%4 == 3
ret = easygui.multenterbox('', '資訊修改', value, list_tem_a)
if ret == None:
return
while len(ret[1]) != 1 or len(ret[2]) != 11 or not ret[2].isdigit():
ret = easygui.multenterbox('格式有誤', '資訊修改', value, list_tem_a) #ret 回傳修改過的資訊
if ret == None:
return
list_employee[num_list[record]]['name'] = ret[0]
list_employee[num_list[record]]['gender'] = ret[1]
list_employee[num_list[record]]['phone'] = ret[2]
list_employee[num_list[record]]['address'] = ret[3]
record = record + 1
list_tem_a = []
with open(filename, 'w') as f:
json.dump(list_employee, f, indent=4)
f.close()
easygui.msgbox('修改成功', title='tip')
def get_subscript(list_tem):
num_list = []
with open(filename) as f:
list_employee = json.load(f)
f.close()
for n in range(len(list_tem)):
if n%4 == 2:
#list_tem[n] == phone
for num in range(len(list_employee)):
if list_employee[num]['phone'] == list_tem[n]:
num_list.append(num)
return num_list
def del_mod(m, t):
i = 0
s = ''
val_p = []
value = []
with open(filename) as f:
data = json.load(f)
f.close()
a = data
if len(data) == 0:
easygui.msgbox('NOT FOUND MESSAGES!')
return
while i < len(data):
for v in data[i].values():
value.append(str(v) + ' ')
i = i + 1
for i in range(len(value)):
s += value[i]
if i % 4 == 3:
val_p.append(s)
s = ''
if len(a) == 1:
return val_p
tem = easygui.multchoicebox(msg=m, title=t,
choices=val_p)
return tem #回傳要修改的原值
def tip_not_found_file():
easygui.msgbox('NOT FOUND MESSAGES!', 'tip')
僅供學習交流!
三,一些廢話
總共不到300行,python的確是一門簡潔的語言,用c的話估計沖著五六百行就去了,python作為輔助語言不錯,但不建議作為第一語言來使用,隱去了很多我們不需要考慮的內容,
主要參考資料:
1,《Python編程從入門到實踐》,人民郵電出版社
2,python簡單圖形界面GUI入門——easygui
https://blog.csdn.net/mingqi1996/article/details/81272621
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/258956.html
標籤:python
上一篇:Jupyter Notebook在清華鏡像下安裝Nbextensions 插件
下一篇:Python入門:條件陳述句
