鱼C论坛

 找回密码
 立即注册
查看: 1490|回复: 0

[技术交流] python 035 easygui模块

[复制链接]
发表于 2018-6-29 16:46:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 风不会停息 于 2018-7-20 16:01 编辑

1. 导入: import easygui as g, easygui模块所有操作方法: http://bbs.fishc.com/thread-46069-1-1.html
    gui(Graphical User Interface  图形用户界面)

附两个 035动动手3, 4 程序:


  1. import easygui as g
  2. import os

  3. file = g.fileopenbox(title = '选择文件', default = "D:\", filetypes = ['*.txt'])
  4. msg = ('文件【%s】的内容如下:' % os.path.split(file)[1])
  5. title = '显示文件内容'

  6. with  open(file) as f:
  7.     file_content = f.read()
  8. reply = g.textbox(msg, title, file_content)

  9. if  reply != file_content + '\n':       #reply 会多一空行
  10.     choice = g.buttonbox('检测到内容发生改变, 请选择以下操作:', '警告', ('覆盖保存', '放弃保存', '另存为...'))

  11.     if  choice == '覆盖保存':
  12.         with  open(file, 'w') as f:
  13.            f.write(reply)
  14.         with  open(file) as f:
  15.             if  f.read() == reply:
  16.                 g.msgbox('保存成功', ok_button = '结束')

  17.     elif  choice == '放弃保存':
  18.         g.msgbox('内容未保存', ok_button = '结束')

  19.     elif  choice == '另存为...':
  20.         save_path = g.filesavebox(title = '选择目录', default = '1_%s' % os.path.split(file)[1])
  21.         with  open(save_path, 'w') as f:
  22.             f.write(reply)
  23.         with  open(save_path) as f:
  24.             if  f.read() == reply:
  25.                 g.msgbox('保存成功', ok_button = '结束')
复制代码

  1. import  easygui as g
  2. import  os

  3. code_extension = ('.asm', '.py', '.c', '.cpp', '.html')
  4. code = dict()
  5. code_lines = code.fromkeys(code_extension, 0)
  6. code_files = code_lines.copy()

  7. def  count_code_lines(path):
  8.     count = 0
  9.     with  open(path, encoding = 'UTF-8') as f:
  10.         print('正在分析文件 %s ...' % path)
  11.         try:
  12.             for each_line in f:
  13.                     count += 1
  14.         except  UnicodeDecodeError:             #忽略掉格式不兼容的文件
  15.             pass
  16.     extension = os.path.splitext(path)[1]
  17.     code_lines[extension] += count

  18. def  count_code_files(path):
  19.     extension = os.path.splitext(path)[1]
  20.     code_files[extension] += 1

  21. def  search_files(path):
  22.     try:
  23.         os.chdir(path)
  24.         for  each_file in os.listdir(os.curdir):
  25.             if  os.path.isdir(each_file):
  26.                 search_files(each_file)
  27.                 os.chdir(os.pardir)
  28.             if  os.path.isfile(each_file):
  29.                 if  os.path.splitext(each_file)[1] in code_extension:
  30.                     count_code_lines(os.getcwd() + os.sep + each_file)
  31.                     count_code_files(os.getcwd() + os.sep + each_file)              #递归搜寻文件
  32.     except  PermissionError:
  33.         pass
  34.    
  35. def  show_result():
  36.     count = 0
  37.     content = ''
  38.     for  each_key in code_files.keys():
  39.          content += ('【%s】源文件 %d 个, 源代码 %d 行\n' % ( each_key, code_files[each_key], code_lines[each_key] ) )
  40.     for  each_value in code_lines.values():
  41.         count += each_value
  42.    
  43.     msg = ("您目前共累积编写了 %d 行代码, 完成进度: %.3f %%\n离 10 万行代码还差 %d 行, 请继续努力!" % ( count, count * 100/100000,  100000 - count) )
  44.     g.textbox(msg = msg, title = '统计代码', text = content)



  45. g.msgbox('请打开您所有存放代码的文件夹', '统计代码量....')
  46. path = g.diropenbox('请选择您的代码库', '浏览文件夹', default = 'D:\\')

  47. search_files(path)
  48. show_result()

复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-26 23:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表