[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()

  

猜你喜欢

转载自www.cnblogs.com/alimy/p/10508278.html