第035讲:图形用户界面入门:EasyGui

动动手

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

在这里插入图片描述

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

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

在这里插入图片描述

import random
import easygui as g

msg = """【*真实姓名】为必填项

【*手机号码】为必填项

【*E-mail】为必填项
"""
title = "账号中心"
Names = [" *用户名", " *真实姓名", "  固定电话", " *手机号码", "  QQ", " *E-mail"]



while 1:
    Values = []
    Values=g.multenterbox(msg,title, Names)
    index=0
    errmsg=''
    for each in Values:
        if each.strip(' ')=='' and Names[index].strip()[0]=='*':
            errmsg+="【%s】为必填项\n" % Names[index]
        index+=1
    if errmsg=='':
        break
    else:
        g.msgbox(errmsg)

print(Values)
  1. 提供一个文件夹浏览框,让用户选择需要打开的文本文件,打开并显示文件内容。
    在这里插入图片描述
import easygui as g
import os

file_path=g.fileopenbox()

with open(file_path,encoding="ANSI") as f:
    
    title=os.path.basename(file_path)
    msg="文件【%s】的内容:" % title
    g.textbox(msg,title,f.read())

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

file_path=g.fileopenbox()

with open(file_path,encoding="ANSI") as f:
    title=os.path.basename(file_path)
    msg="文件【%s】的内容:" % title
    txt=f.read()
    txt_after=g.textbox(msg,title,txt)

if txt_after!=txt:
    choice = g.buttonbox("检测到文件内容发生改变,请选择以下操作:", "警告", ("覆盖保存", "放弃保存", "另存为..."))
    if choice == "覆盖保存":
        with open(file_path,'w',encoding='ANSI') as f:
            f.write(txt_after)
    elif choice == "放弃保存":
        g.msgbox("已放弃保存","警告")
    elif choice == '另存为':
        new_file_path=g.fileopenbox()
        with open(file_path,'w',encoding='ANSI') as f1:
            f1.write(txt_after)
        g.msgbox("已另存","警告")
    else:
        print("什么操作都没有")
**

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

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

在这里插入图片描述
在这里插入图片描述

import easygui as g
import os

target = ['c', 'cpp', 'py', 'cc', 'java', 'pas', 'asm']
file_list={}


def find_file(scanDir):
    for i in os.listdir(scanDir):
        subDir=scanDir+os.sep+i
        if os.path.isdir(subDir):
            find_file(subDir)

        elif i.split('.',1)[1] in target:
            file_list[i]=scanDir

def calculate_line():
    type_line={'c':0, 'cpp':0, 'py':0, 'cc':0, 'java':0 ,'pas':0, 'asm':0}
    for i in file_list:
        with open(file_list[i]+os.sep+i,encoding='utf-8') as f:
            count=0
            for each in f:
                count+=1
        type_line[ i.split('.',1)[1] ]+=count
    return type_line

def showInGui(type_line):
    txt=''
    title='代码行统计'
    count=0
    for i in type_line:
        txt+="%s %d" %(i,type_line[i])+'\n'
        count+=type_line[i]
        
    msg='总数%d行 完%0.2f' %(count,count/100000)
    
    g.textbox(msg,title,txt)


#main
find_file(g.diropenbox())
lines=calculate_line()
showInGui(lines)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值