学习python第十七天(基础篇最后一篇)

本文展示了如何使用Python的easygui库创建简单的猜数字游戏、用户信息登记界面、文件浏览及内容显示功能,以及代码统计程序。通过这些实例,读者可以了解如何在Python中实现基本的图形用户界面(GUI)交互操作。
摘要由CSDN通过智能技术生成

优化最开始的小游戏,加上界面等。

0. 先练练手,把我们的刚开始的那个猜数字小游戏加上界面吧?

代码清单:

  1. import random
  2. import easygui as g
  3. g.msgbox("嗨,欢迎进入第一个界面小游戏^_^")
  4. secret = random.randint(1,10)
  5. msg = "不妨猜一下小甲鱼现在心里想的是哪个数字(1~10):"
  6. title = "数字小游戏"
  7. guess = g.integerbox(msg, title, lowerbound=1, upperbound=10)
  8. while True:
  9.     if guess == secret:
  10.         g.msgbox("我草,你是小甲鱼心里的蛔虫吗?!")
  11.         g.msgbox("哼,猜中了也没有奖励!")
  12.         break
  13.     else:
  14.         if guess > secret:
  15.             g.msgbox("哥,大了大了~~~")
  16.         else:
  17.             g.msgbox("嘿,小了,小了~~~")   
  18.         guess = g.integerbox(msg, title, lowerbound=1, upperbound=10)
  19.             
  20. g.msgbox("游戏结束,不玩啦^_^")


1. 实现一个用于登记用户账号信息的界面(如果是带 * 号的必填项,要求一定要有输入并且不能是空格)


代码清单:

  1. import easygui as g
  2. msg = "请填写以下联系方式"
  3. title = "账号中心"
  4. fieldNames = [" *用户名", " *真实姓名", "  固定电话", " *手机号码", "  QQ", " *E-mail"]
  5. fieldValues = []
  6. fieldValues = g.multenterbox(msg, title, fieldNames)
  7. while 1:
  8.     if fieldValues == None:
  9.         break
  10.     errmsg = ""
  11.     for i in range(len(fieldNames)):
  12.         option = fieldNames[i].strip()
  13.         if fieldValues[i].strip() == "" and option[0] == "*":
  14.             errmsg += ('【%s】为必填项。\n\n' % fieldNames[i])
  15.     if errmsg == "":
  16.         break
  17.     fieldValues = g.multenterbox(errmsg, title, fieldNames, fieldValues)
  18. print("用户资料如下:%s" % str(fieldValues))
  19.    


2. 提供一个文件夹浏览框,让用户选择需要打开的文本文件,打开并显示文件内容。"bmT<wB

代码清单:

  1. import easygui as g
  2. import os
  3. file_path = g.fileopenbox(default="*.txt")
  4. with open(file_path) as f:
  5.     title = os.path.basename(file_path)
  6.     msg = "文件【%s】的内容如下:" % title
  7.     text = f.read()
  8.     g.textbox(msg, title, text)


3. 在上一题的基础上增强功能:当用户点击“OK”按钮的时候,比较当前文件是否修改过,如果修改过,则提示“覆盖保存”、”放弃保存”或“另存为…”并实现相应的功能。

代码清单:

  1. import easygui as g
  2. import os
  3. file_path = g.fileopenbox(default="*.txt")
  4. with open(file_path) as old_file:
  5.     title = os.path.basename(file_path)
  6.     msg = "文件【%s】的内容如下:" % title
  7.     text = old_file.read()
  8.     text_after = g.textbox(msg, title, text)
  9.    
  10. if text != text_after[:-1]:
  11.     # textbox 的返回值会追加一个换行符
  12.     choice = g.buttonbox("检测到文件内容发生改变,请选择以下操作:", "警告", ("覆盖保存", "放弃保存", "另存为..."))
  13.     if choice == "覆盖保存":
  14.         with open(file_path, "w") as old_file:
  15.             old_file.write(text_after[:-1])
  16.     if choice == "放弃保存":
  17.         pass
  18.     if choice == "另存为...":
  19.         another_path = g.filesavebox(default=".txt")
  20.         if os.path.splitext(another_path)[1] != '.txt':
  21.             another_path += '.txt'
  22.         with open(another_path, "w") as new_file:
  23.             new_file.write(text_after[:-1])


4. 写一个程序统计你当前代码量的总和,并显示离十万行代码量还有多远?

  • 要求一:递归搜索各个文件夹
  • 要求二:显示各个类型的源文件和源代码数量
  • 要求三:显示总行数与百分比


代码清单:

  1. import easygui as g
  2. import os
  3. def show_result(start_dir):
  4.     lines = 0
  5.     total = 0
  6.     text = ""
  7.     for i in source_list:
  8.         lines = source_list[i]
  9.         total += lines
  10.         text += "【%s】源文件 %d 个,源代码 %d 行\n" % (i, file_list[i], lines)
  11.     title = '统计结果'
  12.     msg = '您目前共累积编写了 %d 行代码,完成进度:%.2f %%\n离 10 万行代码还差 %d 行,请继续努力!' % (total, total/1000, 100000-total)
  13.     g.textbox(msg, title, text)
  14. def calc_code(file_name):
  15.     lines = 0
  16.     with open(file_name) as f:
  17.         print('正在分析文件:%s ...' % file_name)
  18.         try:
  19.             for each_line in f:
  20.                 lines += 1
  21.         except UnicodeDecodeError:
  22.             pass # 不可避免会遇到格式不兼容的文件,这里忽略掉......
  23.     return lines
  24. def search_file(start_dir) :
  25.     os.chdir(start_dir)
  26.    
  27.     for each_file in os.listdir(os.curdir) :
  28.         ext = os.path.splitext(each_file)[1]
  29.         if ext in target :
  30.             lines = calc_code(each_file) # 统计行数
  31.             # 还记得异常的用法吗?如果字典中不存,抛出 KeyError,则添加字典键
  32.             # 统计文件数
  33.             try:
  34.                 file_list[ext] += 1
  35.             except KeyError:
  36.                 file_list[ext] = 1
  37.             # 统计源代码行数
  38.             try:
  39.                 source_list[ext] += lines
  40.             except KeyError:
  41.                 source_list[ext] = lines
  42.             
  43.         if os.path.isdir(each_file) :
  44.             search_file(each_file) # 递归调用
  45.             os.chdir(os.pardir) # 递归调用后切记返回上一层目录
  46.             
  47. target = ['.c', '.cpp', '.py', '.cc', '.java', '.pas', '.asm']
  48. file_list = {}
  49. source_list = {}
  50. g.msgbox("请打开您存放所有代码的文件夹......", "统计代码量")
  51. path = g.diropenbox("请选择您的代码库:")
  52. search_file(path)
  53. show_result(path)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值