[Python]小甲魚Python視頻第035課(圖形用戶界面入門:EasyGui)課後題及參考解答

# -*- coding: utf-8 -*-
"""
Created on Sat Mar  9 23:22:21 2019

@author: fengs
"""

import easygui as eg


"""
0. 先練練手,把咱們的剛開始的那個猜數字小遊戲加上界面吧?
也能夠直接使用eg.integerbox
"""
def dds0():
    import random as rd
    guess = rd.randint(1,10)
    while True:
        str_input =  eg.enterbox('不妨猜猜是哪一個數字(1~10)','數字小遊戲')
        if str_input.isdecimal():
            int_num = int(str_input)
            if int_num == guess:
                eg.msgbox('回答正確,但並無什麼獎勵')
                break;
            elif int_num > guess:
                eg.msgbox('偏大,改小點試試')
                continue
            else:
                eg.msgbox('偏小,改大點試試')
                continue
        else:
            eg.msgbox('輸入的並不是合法數字,請從新輸入')
            continue


"""
1. 以下圖,實現一個用於登記用戶帳號信息的界面(若是是帶 * 號的必填項,要求必定要有輸入而且不能是空格)。
"""
dds1_title = '帳戶中心'
dds1_msg = """【*真實姓名】必填項.
【*手機號碼】爲必填項.
【*E-mail】爲必選項.
"""
dds1_fieldNames = ['*用戶名','*真實姓名','固定電話','*手機號碼','QQ','*E-mail']
def dds1():
    global dds1_title
    global dds1_msg
    global dds1_fieldNames
    fieldValues = eg.multenterbox(dds1_msg,dds1_title,dds1_fieldNames,[])
    print(fieldValues)
    
#dds1()
    
 
"""
2. 提供一個文件夾瀏覽框,讓用戶選擇須要打開的文本文件,打開並顯示文件內容。
"""
def dds2():
    import os
    file_full_path = eg.fileopenbox(msg=None, title=None, default='*.txt', filetypes=None, multiple=False)
    if None == file_full_path:
        eg.msgbox('並未選中任何txt文件')
    else:
        file_obj = open(file_full_path,'r')
        file_all_context = file_obj.read()
        file_obj.close()
        local_msg = '文件【%s】的內容以下:' % file_full_path.split(os.sep)[-1]
        local_title = '顯示文件內容'
        file_new_context = eg.textbox(local_msg, local_title,file_all_context)
        if file_new_context != None:
            if file_new_context == file_all_context:
                print('Match')
    
#dds2()
                
"""
3. 在上一題的基礎上加強功能:當用戶點擊「OK」按鈕的時候,比較當前文件是否修改過,若是修改過,則提示「覆蓋保存」、」放棄保存」或「另存爲…」並實現相應的功能。
(提示:解決這道題可能須要點耐心,由於你有可能會被一個小問題卡住,但請堅持,本身想辦法找到這個小問題所在並解決它!)
"""

def dds3():
    import os
    file_full_path = eg.fileopenbox(msg=None, title=None, default='*.txt', filetypes=None, multiple=False)
    if None == file_full_path:
        eg.msgbox('並未選中任何txt文件')
    else:
        file_obj = open(file_full_path,'r')
        file_all_context = file_obj.read()
        file_obj.close()
        local_msg = '文件【%s】的內容以下:' % file_full_path.split(os.sep)[-1]
        local_title = '顯示文件內容'
        file_new_context = eg.textbox(local_msg, local_title,file_all_context)
        if file_new_context != None:
            if file_new_context == file_all_context:
                "文件內容並沒有變化"
            else:
                dds3_msg = '檢測到文件內容發生改變,請選擇一下操做:'
                dds3_title = '警告'
                dds3_choices = ['覆蓋保存','放棄保存','另存爲']
                choice = eg.choicebox(dds3_msg, dds3_title, dds3_choices, preselect=0, callback=None, run=True)
                if choice == dds3_choices[0]: #覆蓋保存
                    file_obj = open(file_full_path,'w')
                    file_obj.write(file_new_context[:-1])
                    file_obj.close()
                    eg.msgbox('文件內容已經覆蓋更新完畢')
                elif choice == dds3_choices[1]: #放棄保存
                    pass
                elif choice == dds3_choices[2]: #另存爲
                    new_file_name = eg.filesavebox(msg=None,title='另存爲',default=file_full_path.split(os.sep)[-1],filetypes='*.txt')
                    file_obj = open(new_file_name,'w')
                    file_obj.write(file_new_context[:-1])
                    file_obj.close()
                    eg.msgbox('文件另存爲成功')
                
#dds3()

"""
4. 寫一個程序統計你當前代碼量的總和,並顯示離十萬行代碼量還有多遠?
要求一:遞歸搜索各個文件夾
要求二:顯示各個類型的源文件和源代碼數量
要求三:顯示總行數與百分比
"""
valid_file_suffix = ['.py','.c','.txt'];
file_num_list     = [0,0,0]
code_lines_list   = [0,0,0]

def dds4_statistics( folder_path ):
    import os
    global valid_file_suffix
    global file_num_list
    global code_lines_list
    
    all_file_list = os.listdir(folder_path)
    for each in all_file_list:
        each_full_path = folder_path + os.sep + each
        if os.path.isdir(each):
            dds4_statistics(each_full_path)
        else:
            each_suffix = each.split('.')[-1]
            temp_func = lambda x : x.split('.')[-1]
            if each_suffix in map(temp_func,valid_file_suffix):
                index = list(map(temp_func,valid_file_suffix)).index(each_suffix)
                file_num_list[index] += 1;
                file_obj = open(each_full_path,'r')
                try:
                    file_lines = list(file_obj)
                except UnicodeDecodeError:
                    print('UnicodeDecodeError in %s' % each_full_path)
                    file_obj.close()
                    continue 
                file_obj.close()
                code_lines_list[index] += len(file_lines)
            else:
                pass

#並沒解決這個程序的編碼問題,遇到不認識的編碼文件,會出異常    
def dds4_main():
    global valid_file_suffix
    global file_num_list
    global code_lines_list
    #第一步:得到文件夾
    top_folder_path = eg.diropenbox(msg=None, title=None, default=None)
    #第二步:統計
    if top_folder_path != None:
        dds4_statistics(top_folder_path)
    #第三步:輸出結果
    if top_folder_path != None:
        dds4_msg = "您目前共編寫【%d】行代碼,完成進度:%.2f%% \n 離10萬行代碼還差%d行,請繼續努力!!" % (sum(code_lines_list),100*sum(code_lines_list)/1e5,1e5-sum(code_lines_list))
        dds_title = '統計結果'
        statistice_result = '';
        for i in range(len(valid_file_suffix)):
            statistice_result += '【%s】源文件 %d 個,源代碼 %d 行 \n' % ( valid_file_suffix[i] ,file_num_list[i],code_lines_list[i]);
        eg.textbox(dds4_msg, dds_title,statistice_result)
            #file_new_context = eg.textbox(local_msg, local_title,file_all_context)
    
    
dds4_main()
相關文章
相關標籤/搜索