python 界面二

  • Post author:
  • Post category:python


参照

小甲鱼零基础学习python课后作业

1.输入密码

1.passwordbox的使用 passwordbox(msg, title,default=’默认的密码’, image=None)

import easygui as g
def passwordbox(msg, title):
    msg = g.passwordbox(msg, title,default='默认的密码', image=None)
    print(msg) # 默认的密码

passwordbox('content', 'title')

2.multpasswordbox的使用multpasswordbox(msg, title, fields, values)

import easygui as g
def multpasswordbox(msg, title, fields, values):
    msg = g.multpasswordbox(msg, title, fields, values)
    print(msg) # ['张三', '123456']

fields = ('用户名','密码')
values = ('张三', '123456')
multpasswordbox('输入密码', '标题', fields, values)

2.显示文本

import easygui as g
import os
def readfileshoutext():
    boxtitle = '选择文件'
    boxcontent = '读取文件内容'
    filepath = g.fileopenbox(boxcontent, boxtitle,default = 'D:/*.txt',filetypes='[*.txt]')
    file = open(filepath)
    filetext = file.read()
    texttitle = '文件标题'
    texttip = filepath + ' 的内容为:'
    boxtext = g.textbox(texttip,texttitle,text=filetext,codebox=0)

    if filetext != boxtext[:-1]:
        btn = ('覆盖保存','放弃保存','另存为...')
        choice = g.buttonbox('文件内容发生改变了,请选择如下操作:','警告',btn)
        if choice == '覆盖保存':
            with open(filepath,'w') as oldfile:
                oldfile.write(boxtext[:-1])
                oldfile.close()
        if choice == '放弃保存':
            pass
        if choice == '另存为...':
            newfilepath = g.filesavebox(default='.txt')
            newfile = open(newfilepath,'w')
            newfile.write(boxtext[:-1])
            newfile.close()
readfileshoutext()

改变文件弹出选择

3.目录与文件

1.diropenbox选择目录的路径 diropenbox(msg, title, default = None)

import easygui as g
def diropenbox(msg, title):
    msg = g.diropenbox(msg, title, default = None)
    print(msg) # F:\android_doc 目录地址

diropenbox('目录', '标题')

2.fileopenbox 选择文件的路径 fileopenbox(msg, title, default = ‘*’,filetypes = None)

default = ‘D:/python/program/a*.py’ 表示D盘program下以a开头.py结尾的所有文件

default = ’ ** ’ 表示所有文件

filetypes = [‘*.py’] 显示以.py结尾的文件

filetypes = None 显示所有文件

import easygui as g
def fileopenbox(msg, title):
    msg = g.fileopenbox(msg, title, default = '*',filetypes = None)
    print(msg) # D:\python\program\b.py 文件地址

fileopenbox('选择文件', '标题')

3.filesavebox 选择保存文件的路径 filesavebox(msg, title, default = filename,filetypes = None)

default 文件的名字

import easygui as g
def filesavebox(filetext, filename, msg, title):
    filepath = g.filesavebox(msg, title, default = filename,filetypes = None)
    filecontent = open(filepath,'w')
    filecontent.write(filetext)
    filecontent.close()
    print(filepath) # D:\python\program\a.txt
filename = 'a.txt'
filetext = '这是一个测试文件的内容'
filesavebox(filetext,filename, '选择目录保存文件', '标题')

4.保存设置数据

有个小问题,读出来的是个啥??待了解

import os
from easygui import *

class Settings(EgStore):
    def __init__(para,filename):
        para.user = ''
        para.age = ''
        para.filename = filename
        para.restore()
settings = Settings('D:/seting.txt')
settings.user = '张三'
settings.age = str(20)
settings.store()

a = open('D:/seting.txt','rb')
print(a.read())

5.界面显示异常

import easygui as g
try:
    a = 5/0
except:
    g.exceptionbox()

6.选择某一个目录统计代码行数

import easygui as g
import os

def show(path):
    lines = 0
    total = 0
    text = ''
    for i in soft_list:
        lines = soft_list[i]
        total += lines
        text += '[%s]源文件%d个,源代码%d行\n'%(i,file_list[i],lines)
    text += '它们分别为:\n'
    for key,value in countdict.items():
        text += key + '的代码为%s行' % value + '\n'
    title = '统计结果'
    msg = '目前共%d行代码!' % total
    g.textbox(msg,title,text)


def calc_code(file):
    lines = 0
    f =  open(file,'r',encoding = 'utf-8')
    print('正在计算:%s' % file)
    lines = len(f.readlines())
    return lines

def search_file(file_dir):
    os.chdir(file_dir) #改变当前工作目录到指定的路径
    for item in os.listdir(os.curdir):
        ftype = os.path.splitext(item)[1] # 得到文件后缀名
        if ftype in filetype:
            lines = calc_code(item)
            try:
                file_list[ftype] +=1
            except:
                file_list[ftype] = 1
            #统计代码行数
            try:
                soft_list[ftype] += lines
            except:
                soft_list[ftype] = lines
            countdict.setdefault(item,lines)
        if os.path.isdir(item):
            search_file(item) # 递归
            os.chdir(os.pardir) # 返回上一层目录

filetype = ['.py','.xml','.java']
file_list = {}
soft_list = {}
countdict = {}
path = g.diropenbox('选择要统计的代码文件:') # 文件路径
search_file(path)
show(path)



版权声明:本文为u011269573原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。