EasyGui官網:http://easygui.sourceforge.net
建議不要再IDLE上運行EasyGui,可用notepad++編輯保存再直接用Python運行
下載安裝easygui包之後
導入easygui:
1、直接導入
import easygui
使用直接導入的方法,調用函數時:
easygui.msgbox()
2、導入整個easygui包:
from easygui import *
這樣使得我們更容易調用easygui的函數,可以直接寫函數,但可能與其他函數重名
msgbox()
3、推薦的方式
import easygui as g
這樣可以保持easygui的命名空間,同時方便調用(名字短)
g.msgbox()
使用EasyGui:
0.先練練手,把我們的剛開始的那個猜數字小遊戲加上界面吧?
import easygui as g
import random
g.msgbox('嗨,歡迎進入第一個界面小遊戲!')
secret = random.randint(1,10)
msg = "猜猜我心中的數字是什麼(0~10):"
title = "數字小遊戲"
guess = g.integerbox(msg, title, lowerbound = 0, upperbound = 10)
while True:
if guess == secret:
g.msgbox('猜對了!你是我肚裏的蛔蟲嗎!')
break
else:
if guess > secret:
g.msgbox('猜大了呀,再試試')
else:
g.msgbox('猜小了呀,再試試')
guess = g.integerbox(msg, title, lowerbound = 0, upperbound = 10)
g.msgbox('遊戲結束,不玩啦!')
1.實現一個用於登記用戶賬號信息的界面(如果是帶*號的必填項,要求一定要有輸入並且不能是空格)。
from easygui import *
msg = "輸入你的個人信息"
title = "登錄界面"
fieldNames = ["賬號名*","密碼*","地址","聯繫電話*","郵箱地址"]
fieldValues = [] # 創建一個空列表存放賬號信息
fieldValues = multenterbox(msg,title, fieldNames)
# 確保帶*號的信息不爲空
while 1:
if fieldValues == None:
break
errmsg = ""
for i in range(len(fieldNames)):
if (fieldValues[i].strip() == "") and (fieldNames[i][-1] == "*"):
errmsg = errmsg + ('"%s" 是必填項.\n\n' % fieldNames[i])
if errmsg == "":
break
fieldValues = multenterbox(errmsg, title, fieldNames, fieldValues)
print("個人信息:", fieldValues)
2.提供一個文件瀏覽框,讓用戶選擇需要打開的文本文件,打開並顯示文件內容。
import easygui as g
import os
file_path = g.fileopenbox(default = "*.txt")
with open(file_path) as f:
title = os.path.basename(file_path)
msg = "文件【%s】的內容如下:" %title
text = f.read()
g.textbox(msg, title, text)
3.在上一題的基礎上增強功能:當用戶點擊“OK”按鈕的時候,比較當前文件是否修改過,如果修改過,則提示“覆蓋保存”、“放棄保存”或“另存爲…”並實現相應的功能。
import easygui as g
import os
file_path = g.fileopenbox(default = "*.txt")
with open(file_path) as old_file:
title = os.path.basename(file_path)
msg = "文件【%s】的內容如下:" %title
text = old_file.read()
text_after = g.textbox(msg, title, text)
if text != text_after[:-1]:
#textbox的返回值會追加一個換行符
choice = g.buttonbox("文件已修改,請做出選擇:", "警告", ("覆蓋保存", "放棄保存", "另存爲…"))
if choice == "覆蓋保存":
with open(file_path, "w") as old_file:
old_file.write(text_after[:-1])
if choice == "放棄保存":
pass
if choice == "另存爲…":
another_path = g.filesavebox(default = ".txt")
if os.path.splitext(another_path)[1] != '.txt':
another_path += '.txt'
with open(another_path, "w") as new_file:
new_file.write(text_after[:-1])
4.寫一個程序統計你當前代碼量的總和,並顯示離十萬行代碼量還有多遠?
要求一:遞歸搜索各個文件夾
要求二:顯示各個類型的源文件和源代碼數量
要求三:顯示總行數與百分比
import easygui as g
import os
def show_result(start_dir):
lines = 0
total = 0
text = ""
for i in source_list:
lines = source_list[i]
total += lines
text += "【%s】源文件 %d 個,源代碼 %d 行\n" % (i, file_list[i], lines)
title = '統計結果'
msg1 = '您目前共寫了 %d 行代碼,完成進度:%.2f %%\n離10萬行代碼還差 %d 行,請繼續努力!' % (total, total/1000, 100000 - total)
msg2 = '您目前共寫了 %d 行代碼,完成進度:%.2f %%\n超額完成 %d 行,真棒!' % (total, total/1000, total - 100000)
if total <= 100000:
g.textbox(msg1, title, text)
else:
g.textbox(msg2, title, text)
def calc_code(file_name):
lines = 0
with open(file_name) as f:
print('正在分析文件:%s ...' % file_name)
try:
for each_line in f:
lines += 1
except UnicodeDecodeError:
pass
return lines
def search_file(start_dir):
os.chdir(start_dir)
for each_file in os.listdir(os.curdir):
ext = os.path.splitext(each_file)[1]
if ext in target:
lines = calc_code(each_file)
try:
file_list[ext] += 1
except KeyError:
file_list[ext] = 1
try:
source_list[ext] += lines
except KeyError:
source_list[ext] = lines
if os.path.isdir(each_file):
search_file(each_file)
os.chdir(os.pardir)
target = ['.c', '.cpp', '.py', '.cc', '.java','.pas', '.asm']
file_list = {}
source_list = {}
g.msgbox("請打開您存放所有代碼的文件夾......", "統計代碼量")
path = g.diropenbox("請選擇你的代碼庫:")
search_file(path)
show_result(path)