Learn the Python graphical user interface Easygui, turn a case example, the function is to count the number of lines of code under the specified folder, test the code you wrote to 100,000 lines is still how much distance
First paste the code, the code is not written by themselves, before their own code can not run, very embarrassing, beginners hurt, code to learn to understand the use of Easygui.
ImportEasygui as GImportOSdefShow_result (Start_dir): Lines=0 Total=0 Text="" forIinchSource_list:lines=Source_list[i] Total+=lines Text+=""%s" source file%d, source code%d line \ n"%(i, file_list[i], lines) title='Statistical Results'msg='you have compiled a total of%d lines of code, complete the progress:%.2f%%\n 100,000 lines of code is still less than%d lines, please continue to work! '% (total, total/1000, 100000-Total ) G.textbox (msg, title, text)defCalc_code (file_name): Lines=0 with open (file_name) as F:Print('parsing file:%s ...'%file_name)Try: forEach_lineinchF:lines+ = 1exceptUnicodedecodeerror:Pass #inevitably you will encounter files that are incompatible with the format, which is ignored here ... returnLinesdefSearch_file (Start_dir): Os.chdir (Start_dir) forEach_fileinchOs.listdir (Os.curdir): Ext= Os.path.splitext (each_file) [1] ifExtinchTarget:lines= Calc_code (Each_file)#number of rows of statistics #Do you remember the unusual usage? If the dictionary does not exist and throws Keyerror, the dictionary key is added #number of statistics files Try: File_list[ext]+ = 1exceptKeyerror:file_list[ext]= 1#Count source code lines Try: Source_list[ext]+=LinesexceptKeyerror:source_list[ext]=LinesifOs.path.isdir (each_file): Search_file (each_file)#Recursive invocationOs.chdir (Os.pardir)#remember to return to the previous level of the directory after a recursive callTarget= ['. C','. cpp','. PY','. CC','. CS','. Java','. Pas','. ASM']file_list={}source_list={}g.msgbox ("Please open the folder where you store all your code ...","Statistics Code Amount") path= G.diropenbox ("Please select your code base:") search_file (path) show_result (path)
Test procedures, write-down diagrams
Haha, do not be my code to scare, in fact, my Search Folder is I store a variety of Reference instance code folder, the program in Idle run an estimated three minutes.
Personal code volume but million, novice injured, slowly climb, slowly efforts ~
Python Learning Note: A simple example of Easygui