小甲鱼Python第035讲:图形用户界面入门:EasyGui | 课后测试题及参考答案
动动手:
0.先练练手,把我们的刚开始的那个猜数字小游戏加上界面吧?
实现代码:
1 import random 2 import easygui as g 3 secret = random.randint(1,10) 4 g.msgbox("欢迎进入猜数字小游戏") 5 msg = "不妨猜一下小甲鱼现在心里想的是哪个数字(1~10):" 6 title = "数字小游戏" 7 guess = g.integerbox(msg ,title ,lowerbound= 1,upperbound= 10 ) 8 # print("猜数字小游戏") 9 # guess = int(input("请输入你猜测的数字:")) 10 if guess == secret: 11 print("一次就猜对,太厉害了!") 12 while guess != secret: 13 if guess == secret: 14 g.msgbox("猜对了") 15 break 16 elif guess > secret: 17 g.msgbox("大了") 18 guess = g.integerbox(msg ,title ,lowerbound= 1,upperbound= 10 ) 19 # int(input("猜错了,请输入你猜测的数字:")) 20 elif guess < secret: 21 g.msgbox("小了") 22 guess = g.integerbox(msg, title, lowerbound=1, upperbound=10) 23 # print("小了") 24 # guess = int(input("猜错了,请输入你猜测的数字:")) 25 g.msgbox("恭喜你猜对了,游戏结束。")
1.如下图,实现一个用于等级用户账号信息的界面(如果是带*号的必填项,要求一定要有输入并且不能是空格)。
实现代码:
1 import easygui as g 2 msg = ''' 3 [*真实姓名]为必填项。 4 5 [*手机号码]为必填项。 6 7 [*E-mail]为必填项。 8 9 ''' 10 title = "账号中心" 11 fieldsNames = ["*用户名","*真实姓名","固定电话","*手机号码","Q Q","*E-mail"] 12 fieldsValues = [] 13 fieldsValues = g.multenterbox( msg ,title, fieldsNames) 14 15 while True: 16 if fieldsValues == None: 17 break 18 errmsg = "" 19 for i in range(len(fieldsNames)): 20 if "*" in fieldsNames[i]: 21 option = fieldsValues[i].strip() 22 if option == "": 23 errmsg += ('[%s]此项为必填项\n'% fieldsNames[i]) 24 g.msgbox(errmsg) 25 # print(fieldsValues[i]) 26 if errmsg == "": 27 break 28 fieldsValues = g.multenterbox(msg, title, fieldsNames,fieldsValues) 29 print("用户资料如下:%s" % str(fieldsValues))
2.提供一个文件夹浏览框,让用户选择需要打开的文本文件,打开并显示文件内容。
实现代码:
1 import easygui as g 2 import os 3 4 file_path = g.fileopenbox(default = "*.txt" ) 5 with open(file_path) as f: 6 title = "显示文件内容" 7 file_name = os.path.basename(file_path) 8 msg =( "文件【%s】的内容如下:" % title ) 9 text = f.read() 10 11 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 = "显示文件内容" 6 file_name = os.path.basename(file_path) 7 msg =( "文件【%s】的内容如下:" % title ) 8 text = old_file.read() 9 new_file_text = g.textbox(msg,title,text) 10 print(new_file_text) 11 if text != new_file_text: 12 choice = g.buttonbox(msg = "检测到文件内容发生改变,请选择一下操作;",title = "警告",choices =("覆盖保存","放弃保存","另存为...")) 13 if choice == "覆盖保存": 14 with open(file_path,"w") as old_file: 15 old_file.write(new_file_text) 16 if choice =="放弃保存": 17 pass 18 if choice == "另存为...": 19 anothor_path = g.filesavebox(default=".txt") 20 # 确保保存路径下文档默认为.txt文档 21 if os.path.splitext(anothor_path)[-1] != ".txt": 22 anothor_path += ".txt" 23 with open(anothor_path,"w") as new_file: 24 new_file.write(new_file_text)
4.写一个程序统计你当前代码量的综合,并显示离十万行代码量还有多远?
- 要求一:递归搜索各个文件夹
- 要求二:显示各个类型的源文件和源代码数量
- 要求三:显示总行数与百分比
截图一:
实现代码:还不会!!
1 import easygui as g 2 import os 3 4 def show_result(start_dir): 5 lines = 0 6 total = 0 7 text = "" 8 9 for i in source_list: 10 lines = source_list[i] 11 total += lines 12 text += "[%s]源文件%d个,源代码%d行\n" % (i, file_list[i], lines) 13 # = " 14 title = "统计结果" 15 msg = "您目前共累计编写了%d行代码,完成进度:%.2f%%\n离十万行代码还差%d行,请继续努力!"% (total,total/1000,100000-total) 16 # for each in text: 17 g.textbox(msg,title, text) 18 19 # 在一个文件中查找代码量 20 def calc_code(file_name): 21 lines = 0 22 with open(file_name,"rb") as f: 23 print("正在分析文件:%s..." % file_name) 24 try: 25 for each_line in f: 26 lines += 1 27 except UnicodeDecodeError: 28 # 不可避免格式不兼容的文件,这里忽略掉··· 29 pass 30 # print(lines) 31 return lines 32 33 # 查找文件 34 def search_file(start_dir): 35 os.chdir(start_dir) 36 for each_file in os.listdir(os.curdir): 37 38 ext = os.path.splitext(each_file)[-1] 39 if ext in target: 40 lines = calc_code(each_file) 41 42 # 统计文件数 43 try: 44 file_list[ext] += 1 45 except KeyError: 46 file_list[ext] = 1 47 # 统计源代码行数 48 try: 49 source_list[ext] += lines 50 except KeyError: 51 source_list[ext] = lines 52 if os.path.isdir(each_file): 53 search_file(each_file) # 递归调用 54 os.chdir(os.pardir) # 递归调用后切记返回上一层目录 55 56 target = ['.c','.cpp','.py','.cc','.java','.pas','.asm'] 57 file_list = { } 58 source_list = { } 59 60 g.msgbox("请打开您存放所有代码的文件夹···","统计代码量") 61 path = g.diropenbox("请选择您的代码库:") 62 63 search_file(path) 64 show_result(path)