鱼C论坛

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

[作品展示] 简易用户信息收集器

[复制链接]
发表于 2022-7-28 00:00:18 | 显示全部楼层 |阅读模式

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

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

x
本人新手,请大家多多支持
#简易用户信息收集程序
#请不要随意在name里添加文字
#如果您要测试代码,请修改77行的 字典.txt 的地址,这里还没有完善,谅解!
#窗口大小以及字体都没改,有时间再调整
#其中小部分代码借鉴于小甲鱼课后作业的思路
#写了部分注释,不明白的可以直接问
#信息可以保存
#有建议改进方法,创新设计或者bug随时指出,谢谢!
#本人自学入门一个月刚刚初三毕业,第一个用心写的项目,没什么创意,请大家多多包涵


import easygui as g
import os



def save():
        msg="请填写内容"
        title="用户信息填写"
        problem=[" *用户名"," *真实姓名"," 固定电话"," *手机号码"," QQ"," *E-mail"]
        Inp=g.multenterbox(msg,title,problem)

        while True:
                next_problem=""#这是一个收集带未填写的带'*'的信息栏的名称
                if Inp==None:#如果点了Cancle(取消),Inp就等于None
                        break
                for i in range(len(Inp)):
                        problem_=problem[i].strip()   #" *用户名"左边有一个空格,为了美观,在下面判断是否有*,要去空格。不过也可以 if "*" in problem[i]
                        if problem_[0]=="*" and Inp[i]=="":
                                next_problem+="%s不能为空\n\n"%problem_

                if next_problem=="" and Inp[0] not in name:   #如果带*的都填了,而且用户没和name里的重复(这里的name是个字典,key=用户名,value=TXT的path,每一个用户信息都有一个txt文档保存起来的)
                        a=g.filesavebox(msg="选择文件夹",title="另存为")
                        name[Inp[0]]=a+".txt"   #存入字典
                        with open(a+".txt","w") as f:
                                
                                f.write(str(Inp)+"\n")
                        break

                Inp=g.multenterbox(next_problem,title,problem,Inp)

def read():
        while True:
                read_=g.enterbox(msg="请输入查找对象的文件名")
                if read_ in name:
                        if "\n" in name[read_]:   #判断"\n"是因为储存在name里的用户名地址里有"\n",防止地址出错,详细的解释继续看
                                path_=name[read_].replace("\n","")
                        else:
                                path_=name[read_]
                        try:        #这里是解决打开读取用户信息时有中文的情况,有中文的txt的编码是ANSI
                                with open(path_,encoding='utf-8') as f:   #utf-8,纯英文字母的
                                        text=f.read()
                                        msg=read_
                                        title="读取"
                                        g.textbox(msg,title,text)
                                        break
                        except:
                                with open(path_,encoding='gbk') as f:#utf-8   #gbk,处理用户信息的中文
                                        text=f.read()
                                        msg=read_
                                        title="读取"
                                        g.textbox(msg,title,text)
                                        break
                else:
                        g.msgbox("没有找到此用户信息")
                        break
                
def del_():
        del_inp=g.enterbox(msg="请输入你要删除的用户的用户名:")
        if del_inp in name:
                if "\n" in name[del_inp]:   #也是为了避免路径错误
                        path_=name[del_inp].replace("\n","")
                else:
                        path_=name[del_inp]
                os.remove(path_)   #先删除用户信息txt
                del name[del_inp]   #再删除name里的用户
                
                
#函数部分                        
#------------------------------
#准备name
name={}
dict_path="D:\\py\\新建文件夹\\字典.txt"   #首先先将字典.txt里的内容一行一行提取并且存入字典,每一行都是键和值,两者用空格分开
with open(dict_path,"r",encoding='gbk') as dict_name:#  防止中文
        if dict_name.read()!="":   #如果是初次打开就是 ""
                dict_name.seek(0)   #回归指针
                for line in dict_name.readlines():
                        line_=line.split(" ")#以空格分开
                        k=line_[0]
                        v=line_[1]
                        name[k]=v
                        

while True:
        
        msg="请选择操作【save:存入】【read:查看】【exit:退出】【del:删除】:"
        title="初始界面"
        choices=('save[1]','read[2]','del[3]','exit[4]')           #[]
        first_inp=g.buttonbox(msg,title,choices)
        if first_inp=="save[1]":
                save()
        elif first_inp=="read[2]":
                read()
        elif first_inp=="del[3]":
                del_()
        elif first_inp=="exit[4]":
                break
        else:
                g.msgbox("输入错误【save:存入】【read:查看】【exit:退出】【del_:删除】",ok_button="好的")
        
#将name里的信息存入 字典.txt
with open(dict_path,"w") as dict_name:
        for k,v in name.items():
                if k!=list(name.keys())[-1] and "\n" not in v:
                        dict_name.write(str(k)+" "+str(v)+"\n")
                else:
                        dict_name.write(str(k)+" "+str(v))
g.msgbox("成功退出")

评分

参与人数 2荣誉 +1 贡献 +3 收起 理由
python爱好者. + 1 鱼C有你更精彩^_^
青出于蓝 + 3 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-28 10:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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