猫史档案馆


【便签】‘回 BZIClaw’

用户:wwwloadwwwload查看:117 回复:4 评论:18 创建时间:2021-08-10T12:06:39


这是一个模拟便签的作品,主要核心代码是写入和读取

代码献上

# 导包
import easygui
import sys
import time
import json

# 开头
print('---------便签本----------')
print('选择数字1写便签 选择数字2查询便签')
print('输入数字3查看所有便签 选择Q退出程序')
# 时间
time_now = time.strftime("%Y-%m-%d", time.localtime())

# 选择方式
mode = ['1', '2', '3', 'Q']
while True:
	answer = easygui.choicebox('请选择', '选择', mode)
	if answer is None:
		sys.exit()
	else:
		if answer == '1':
			content = easygui.enterbox('请输入你想写的便签', '写入')
			info = {'content': str(content), 'time': str(time_now)}
			if content is None:
				sys.exit()
			elif content == '':
				easygui.msgbox('不可以是空字符')
			else:
				with open('content.txt', 'a', encoding='utf-8') as f:
					new_info = json.dumps(info)
					f.write(new_info + '\n')
		elif answer == 'Q':
			sys.exit()
		elif answer == '2':
			with open('content.txt', 'r', encoding='utf-8') as f:
				data = f.readlines()
			if not data:
				easygui.msgbox('便签为空')
			else:
				new_data_list = []
				for c in data:
					new_data = c.strip()
					new_data_list.append(json.loads(new_data))
				num = ['']
				for i in range(0, len(new_data_list)):
					num.append(i + 1)
				choice_num = easygui.choicebox('请选择你要查询的便签', '选择', num)
				if choice_num is None:
					sys.exit()
				elif choice_num == '':
					easygui.msgbox('你还真选空字符啊')
				else:
					easygui.msgbox('时间: ' + str(new_data_list[int(choice_num) - 1]['time']) + '  内容: ' + str(new_data_list[int(choice_num) - 1]['content']))
		elif answer == '3':
			with open('content.txt', 'r', encoding='utf-8') as f:
				data = f.readlines()
			if not data:
				easygui.msgbox('便签为空')
			else:
				new_data_list = []
				for c in data:
					new_data = c.strip()
					new_data_list.append(json.loads(new_data))
				for i in range(0, len(new_data_list)):
					easygui.msgbox('时间: ' + str(new_data_list[i]['time']) + '  内容: ' + str(new_data_list[i]['content']))

 

 

一个需要注意的地方需要创建一个和这个代码项目文件同目录的content.txt文件

如果不想太麻烦也可以直接在gitee上克隆下载

gitee网址

h t t p s : / / g i t e e . c o m / w w w l o a d / n o t e 

 

请自行删除空格


回复

上一页第 1 页 / 共 1 页下一页
伴雪纷飞伴雪纷飞

这都1202年了,谁还用easygui呀

点赞1


评论

9494()

还有这格式..

easygui 我也没说不可以用

?该评论是评论这个帖子的

对呀,这个贴是完成我的教学文档的一个小任务

?‪‪

总之不是评论你的拉

az,input得进行一大堆的判断所以我才不想用,tkinter我这压根不会用,只好用easygui

好吧()


BZIClawLYNEYBZIClawLYNEY

Gitee 网址就不用了,我有

另外谢谢您对我的第一个教学文档的关注

emotion_编程猫_谢谢老板emotion_编程猫_谢谢老板emotion_编程猫_谢谢老板

点赞0


评论

不客气,可以出多点作业,嘿嘿,最近没什么项目好做


BZIClawLYNEYBZIClawLYNEY

我已经检查了,代码有些小问题

首先是你没有用 file.close

其次是我选择 2 后自动退出了

感谢您对文档的关注~~

点赞0


评论

用了with as了啊

也行呀,with 和 open 都可以

对啊 有with其实不用close

其实不喜欢用file.close,就是不擅长用,所以经常不写

em.不用写

我倒是不写,只要没错误就行


BZIClawLYNEYBZIClawLYNEY

电子便签任务的代码如下:

import datetime

notes = []
note = {}

newNote = open("note.txt", "w")
newNote.close()

print("================= 便签本 =================")
while True:
    choice = input("输入 1 写便签,输入 2 查询便签\n输入 3 浏览全部便签,输入 q 退出系统\n")
    if choice.strip("") == "q":
        print("以退出电子便签本软件系统~~")
        break
    elif choice.strip("") == "1":
        order = datetime.datetime.now()
        order = format(order, "%Y-%m-%d %H:%M:%S")
        word = input("")
        note[order] = word
        file = open("note.txt", "a")
        file.write(order + "&" + word + "\n")
        file.close()
    elif choice.strip("") == "2":
        string = input("请输入要查询的文字:")
        file = open("note.txt")
        while True:
            line = file.readline().strip('').strip("\n")
            if line == "":
                break
            if string in line:
                lines = line.split("&")
                print(lines[0])
                print("    " + lines[1])
        file.close()
    elif choice.strip("") == "3":
        file = open("note.txt")
        while True:
            line = file.readline().strip('').strip("\n")
            if line == "":
                break
            lines = line.split("&")
            print(lines[0])
            print("    " + lines[1])
        file.close()

请查收~~

点赞0


评论

呃,已退出电子便签系统,不是 以

你这个输入数字有点问题好像,我输入了数字后没有任何的反应,我输入的数字实在范围内的