(标答出处: 鱼C论坛)
《零基础入门学习Python》
动动手:
import easygui as g
import sys
import random
g.msgbox("(。・∀・)ノ゙嗨,欢迎来到第一个小游戏!")
secret = random.randint(1,10)
msg = "不妨猜一下小甲鱼现在心里想的是那个数字(1-10):"
title = "数字小游戏"
guess = g.integerbox( msg, title ,lowerbound=0, upperbound=10)
while 1 :
if guess == secret :
g.msgbox("卧槽,你是小甲鱼心里的蛔虫吗?!")
g.msgbox("猜中了也没奖励!")
break
else :
if guess > secret :
g.msgbox("哥,大了大了~~")
else :
g.msgbox("哥,小了小了~~")
guess = g.integerbox( msg, title ,lowerbound=0, upperbound=10)
g.msgbox("游戏结束,不玩了,不玩了^_^")
import easygui as g
import sys
import random
valus = []
msg = "请填写以下联系方式"
title = "账号中心"
fields = '*用户名','*真实姓名','固定电话','*手机号码','QQ','*E-mail'
values = g.multenterbox(msg,title, fields)
while 1 :
if valus == None :
break
errmsg = ""
for i in range (len (fields)) :
option = fields[i].strip()
if values[i].strip() == "" and option[0] == "*" :
errmsg += ("[%s]为必填项。\n\n" % fields[i])
if errmsg == "" :
break
values = g.multenterbox(errmsg,title,fields,values)
print ("用户资料如下:%s" % str(values))
import easygui as g
import os
import random
wenjianmulu = g.fileopenbox(msg=None, title=None, default='*.txt', filetypes=None)
with open (wenjianmulu) as f :
title = os.path.basename(wenjianmulu)
msg = "文件【%s】的内容如下:" %title
text = f.read()
g.textbox(msg, title, text, codebox=0)
import easygui as g
import os
import random
wenjianmulu = g.fileopenbox(msg=None, title=None, default='*.txt', filetypes=None)
with open (wenjianmulu) as old_f :
title = os.path.basename(wenjianmulu)
msg = "文件【%s】的内容如下:" %title
text = old_f.read()
later_text = g.textbox(msg, title, text, codebox=0)
if text != later_text[ : -1 ] :
#textbox 的返回值会追加一个空行
choice = g.buttonbox("检测到文件内容发生变化,请选择一下操作:","警告",("覆盖保存","放弃保存","另存为..."))
if choice == "覆盖保存" :
with open (wenjianmulu , "w") as old_f :
old_f.write(later_text[ : -1 ])
if choice == "放弃保存" :
pass
if choice == "另存为..." :
another_wenjianmulu = g.filesavebox(default=".txt")
if os.path.splitext(another_file)[1] != '.txt' :
another_file += '.txt'
with open (another_wenjianmulu, 'w') as new_file :
new_file.write(later_text[ : -1 ])
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 = "统计结果"
msg = "您目前共积累编写了 %d 行代码,完成进度 %.2f %%\n 离10万行代码还差 %d 行,请继续努力!" %(total,total/1000,100000-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','.cpp','.py','.cc','.java','.pas','.asm']
file_list = {}
source_list = {}
g.msgbox("请打开您存放所有代码的文件夹……","统计代码量")
path = g.diropenbox("请选择您的代码库:")
search_file(path)
show_result(path)