# 0.把刚开始的猜数字小游戏加上界面
import random
import easygui as g
secret = random.randint(1,10)
g.msgbox("嗨,欢迎进入第一个界面小游戏~")
guess= g.integerbox(msg = '猜一下小甲鱼心里想的是哪个数字(1-10):',titie = '数字小游戏',
lowerbound = 1,upperbound = 10)
while True:
if guess == secret:
g.msgbox("哎呀!你是小甲鱼肚里的蛔虫吗?!")
g.msgbox("哼~猜中了也没有奖励!")
break
else:
if guess > secret:
g.msgbox("大了大了")
else:
g.msgbox("小了小了")
guess = g.integerbox(msg = '猜一下小甲鱼心里想的是哪个数字(1-10):',titie = '数字小游戏',
lowerbound = 1,upperbound = 10)
g.msgbox("游戏结束,不玩啦")
#1.实现一个用于等级用户账号信息的界面(如果是带*号的必填项,要求一定要有输入并且不能是空格)。
import easygui as g
msg = '请填写以下联系方式'
title = '账号中心'
fieldNames = ['*用户名','*真实姓名','固定电话','*手机号码','QQ','*E-mail']
fieldValue = []
fieldValue = g.multenterbox(msg,title,fields=(fieldNames))
while 1:
if fieldValue == None:
break
errmsg = ''
for i in range(len(fieldNames)):
option = fieldNames[i].strip() #拆分字符串
if fieldValue[i].strip() == '' and option[0] == '*':#带*号的必填项为空时
errmsg += ('【%s】为必填项。\n\n '%fieldNames[i])
if errmsg == '':
break
fieldValue = g.multenterbox(errmsg,title,fieldNames,fieldValue)
print('用户资料如下:%s'%str(fieldValue))
#2.提供一个文件夹浏览框,让用户选择需要打开的文本文件,打开并显示文件内容。
import easygui as g
import os
file_path = g.fileopenbox(msg='请选择文件', 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(msg='请选择文件', 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("检测到文件内容发生改变,请选择以下操作:",'警告',choices = ("覆盖保存","放弃保存",
"另存为..."))
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 os
import easygui as g
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 = '统计结果'
msg = '您目前共累计编写了%d行代码,完成进度:%.2f %%\n离十万行代码还差%d行,请继续努力!'%(total,total/10000,10000-total)
g.textbox(msg,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','.py','.java','.js','.r']
file_list = {}
source_list = {}
g.msgbox('请打开您存放所有代码的文件夹......','统计代码量')
path = g.diropenbox('请选择您的代码库:')
search_file(path)
show_result(path)