鱼C论坛

 找回密码
 立即注册
查看: 732|回复: 6

[已解决]easygui的课后第三题

[复制链接]
发表于 2020-4-18 10:03:56 | 显示全部楼层 |阅读模式

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

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

x
答案中给的代码抄了一遍,为啥运行结果不对啊?每次不管选择“覆盖保存”,“放弃保存”或者“不保存”,我原来文档中的最后一个字总是会被删掉。这是为什么?
  1. import easygui as eg
  2. import os

  3. f=open('C:\\Users\\hp\\Desktop\\juli.txt',errors='ignore',encoding='utf-8')
  4. title='juli'
  5. msg='文件【juli】的内容如下:'
  6. text=f.read()
  7. f.seek(0,0)
  8. text_after=eg.textbox(msg,title,text=f.read())
  9. f.close()

  10. if text!=text_after[:-1]:
  11.     jieguo=eg.buttonbox('检测到文件内容发生改变,请选择以下操作:',\
  12.                         title='警告',choices=['覆盖保存','放弃保存','另存为...'])
  13.     if jieguo=='覆盖保存':
  14.         a=open('C:\\Users\\hp\\Desktop\\juli.txt','w',errors='ignore',encoding='utf-8')
  15.         a.write(text_after[:-1])
  16.         a.seek(0,0)
  17.         a.close()
  18.     if jieguo=='放弃保存':
  19.         pass
  20.     if jieguo=='另存为...':
  21.         another_path=eg.filesavebox(default='C:\\Users\\hp\\Desktop\\.txt')
  22.         if os.path.splitext(another_path)[1]!='C:\\Users\\hp\\Desktop\\.txt':
  23.             another_path+='.txt'
  24.         with open(another_path,'w') as newfile:
  25.             newfile.write(text_after[:-1])

复制代码
最佳答案
2020-4-18 10:05:37
  1. import easygui as g
  2. import os

  3. file_path = g.fileopenbox(default="*.txt")

  4. with open(file_path, encoding='utf-8') as old_file:
  5.     title = os.path.basename(file_path)
  6.     msg = "文件【%s】的内容如下:" % title
  7.     text = old_file.read()
  8.     text_after = g.textbox(msg, title, text)
  9.    
  10. if text != text_after: #text_after[:-1]
  11. #小甲鱼的答案说textbox 的返回值会追加一个换行符,我运行的结果不会,故将text_after[:-1]改为了text_after
  12.    
  13.     choice = g.buttonbox("检测到文件内容发生改变,请选择以下操作:", "警告", ("覆盖保存", "放弃保存", "另存为..."))
  14.     if choice == "覆盖保存":
  15.         with open(file_path, "w", encoding='utf-8') as old_file:
  16.             old_file.write(text_after)  #text_after[:-1]
  17.     if choice == "放弃保存":
  18.         pass
  19.     if choice == "另存为...":
  20.         another_path = g.filesavebox(default=".txt")
  21.         if os.path.splitext(another_path)[1] != '.txt':
  22.             another_path += '.txt'
  23.         with open(another_path, "w", encoding='utf-8') as new_file:
  24.             new_file.write(text_after)  #text_after[:-1]
复制代码

应该是版本的问题,详见https://blog.csdn.net/qq_41556318/article/details/84318433
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-18 10:05:37 | 显示全部楼层    本楼为最佳答案   
  1. import easygui as g
  2. import os

  3. file_path = g.fileopenbox(default="*.txt")

  4. with open(file_path, encoding='utf-8') as old_file:
  5.     title = os.path.basename(file_path)
  6.     msg = "文件【%s】的内容如下:" % title
  7.     text = old_file.read()
  8.     text_after = g.textbox(msg, title, text)
  9.    
  10. if text != text_after: #text_after[:-1]
  11. #小甲鱼的答案说textbox 的返回值会追加一个换行符,我运行的结果不会,故将text_after[:-1]改为了text_after
  12.    
  13.     choice = g.buttonbox("检测到文件内容发生改变,请选择以下操作:", "警告", ("覆盖保存", "放弃保存", "另存为..."))
  14.     if choice == "覆盖保存":
  15.         with open(file_path, "w", encoding='utf-8') as old_file:
  16.             old_file.write(text_after)  #text_after[:-1]
  17.     if choice == "放弃保存":
  18.         pass
  19.     if choice == "另存为...":
  20.         another_path = g.filesavebox(default=".txt")
  21.         if os.path.splitext(another_path)[1] != '.txt':
  22.             another_path += '.txt'
  23.         with open(another_path, "w", encoding='utf-8') as new_file:
  24.             new_file.write(text_after)  #text_after[:-1]
复制代码

应该是版本的问题,详见https://blog.csdn.net/qq_41556318/article/details/84318433
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-18 10:06:29 | 显示全部楼层
题目是:打开文本文件,并显示文件内容;当用户点击‘OK’按钮时,比较当前文件是否修改过,如果修改过,则提示‘覆盖保存’,‘放弃保存或‘另存为...’’
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-18 10:17:00 | 显示全部楼层
老八秘制 发表于 2020-4-18 10:05
应该是版本的问题,详见https://blog.csdn.net/qq_41556318/article/details/84318433

对了!谢谢你
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-18 10:20:12 | 显示全部楼层

设个最佳谢谢~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-18 10:38:40 | 显示全部楼层

不好意思啊,我这个没设成‘问题求助’,所以选不了最佳答案。给你的回答置顶啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-18 12:19:50 | 显示全部楼层
绵绵haha 发表于 2020-4-18 10:38
不好意思啊,我这个没设成‘问题求助’,所以选不了最佳答案。给你的回答置顶啦

帮你改成问题求助了,设他为最佳吧
P.S:可以自己编辑帖子改帖子类型的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-16 17:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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