python 小甲鱼 os read_[Python]小甲鱼Python视频第035课(图形用户界面入门:EasyGui)课后题及参考解答...

本文介绍使用Python easygui库创建简易图形用户界面的应用案例,包括数字猜测游戏、用户信息登记、文件浏览与内容显示等功能,并实现代码量统计工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值