鱼C论坛

 找回密码
 立即注册
查看: 1173|回复: 7

[已解决]Python第35讲课后习题求助

[复制链接]
发表于 2020-5-12 15:34:10 | 显示全部楼层 |阅读模式

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

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

x
3. 在上一题的基础上增强功能:当用户点击“OK”按钮的时候,比较当前文件是否修改过,如果修改过,则提示“覆盖保存”、”放弃保存”或“另存为…”并实现相应的功能。
以下是我编写的代码:
import easygui as g
file_name = g.fileopenbox(msg = '请选择要阅读的文本文件:',title = '文本阅读程序')
with open(file_name,'r') as f:
    file_content = f.read()
    content = g.textbox(msg = '文件【%s】的内容如下:' % file_name,title = '显示文件内容:',text = file_content)
if file_content != content:
    choice = g.buttonbox(msg = '检测到文件内容发生改变,请选择以下操作:',title = '警告',choices = '覆盖保存','放弃保存','另存为...')
    if choice == '覆盖保存':
        f = open(file_name,'w')
        f.writelines(content)
        f.close()
    elif choice == '放弃保存':
        return
    elif choice == '另存为...':
        new_location = g.filesavebox()
        f2 = open(new_location,'w')
        f2.writelines(content)
        f2.close()
保存执行出现下图的问题,可我看不明白关键字参数的位置哪里错误,请大佬指点!
最佳答案
2020-5-12 16:22:35
实际上,解决了表面的两个问题,你的代码还有两个问题:
1.选择文件窗口选择“取消”程序会出错;
2.显示文件内容选择“cancel”会错误认为文件内容改变,因为cancel后content为空;
所以还得加几个判断来解决这些问题:
  1. import easygui as g
  2. file_name = g.fileopenbox(msg = '请选择要阅读的文本文件:',title = '文本阅读程序')
  3. if file_name:         #<---选“取消”file_name为空
  4.     with open(file_name,'r') as f:
  5.         file_content = f.read()
  6.         content = g.textbox(msg = '文件【%s】的内容如下:' % file_name,title = '显示文件内容:',text = file_content)
  7.     if content and file_content != content:     #<---选"cancel"content为空
  8.         choice = g.buttonbox(msg = '检测到文件内容发生改变,请选择以下操作:',title = '警告',choices = ('覆盖保存','放弃保存','另存为...'))
  9.         if choice == '覆盖保存':
  10.             f = open(file_name,'w')
  11.             f.writelines(content)
  12.             f.close()
  13.         elif choice == '另存为...':
  14.             new_location = g.filesavebox()
  15.             f2 = open(new_location,'w')
  16.             f2.writelines(content)
  17.             f2.close()
复制代码
SharedScreenshot.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-12 15:37:04 | 显示全部楼层
本帖最后由 qiuyouzhi 于 2020-5-12 15:56 编辑

报错行上面,choice那里,把那后三个字符串用括号括起来试试
而且return不能那么用,可以改成exit
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-12 16:00:29 | 显示全部楼层
还有你那个return应该在def里面啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-12 16:07:53 | 显示全部楼层
  1. import easygui as g
  2. file_name = g.fileopenbox(msg = '请选择要阅读的文本文件:',title = '文本阅读程序')
  3. with open(file_name,'r') as f:
  4.     file_content = f.read()
  5.     content = g.textbox(msg = '文件【%s】的内容如下:' % file_name,title = '显示文件内容:',text = file_content)
  6. if file_content != content:
  7.     choice = g.buttonbox(msg = '检测到文件内容发生改变,请选择以下操作:',title = '警告',choices = ('覆盖保存','放弃保存','另存为...'))     #<---choices的参数要用括号括起来
  8.     if choice == '覆盖保存':
  9.         f = open(file_name,'w')
  10.         f.writelines(content)
  11.         f.close()
  12. #     elif choice == '放弃保存':   #<---实际上选择这项就是什么都不用做,所以都不用写,选了它就顺其自然退出程序了。
  13. #         return                          #<---return只能用在函数里返回
  14.     elif choice == '另存为...':
  15.         new_location = g.filesavebox()
  16.         f2 = open(new_location,'w')
  17.         f2.writelines(content)
  18.         f2.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-12 16:22:35 | 显示全部楼层    本楼为最佳答案   
实际上,解决了表面的两个问题,你的代码还有两个问题:
1.选择文件窗口选择“取消”程序会出错;
2.显示文件内容选择“cancel”会错误认为文件内容改变,因为cancel后content为空;
所以还得加几个判断来解决这些问题:
  1. import easygui as g
  2. file_name = g.fileopenbox(msg = '请选择要阅读的文本文件:',title = '文本阅读程序')
  3. if file_name:         #<---选“取消”file_name为空
  4.     with open(file_name,'r') as f:
  5.         file_content = f.read()
  6.         content = g.textbox(msg = '文件【%s】的内容如下:' % file_name,title = '显示文件内容:',text = file_content)
  7.     if content and file_content != content:     #<---选"cancel"content为空
  8.         choice = g.buttonbox(msg = '检测到文件内容发生改变,请选择以下操作:',title = '警告',choices = ('覆盖保存','放弃保存','另存为...'))
  9.         if choice == '覆盖保存':
  10.             f = open(file_name,'w')
  11.             f.writelines(content)
  12.             f.close()
  13.         elif choice == '另存为...':
  14.             new_location = g.filesavebox()
  15.             f2 = open(new_location,'w')
  16.             f2.writelines(content)
  17.             f2.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-12 16:53:45 | 显示全部楼层
txxcat 发表于 2020-5-12 16:22
实际上,解决了表面的两个问题,你的代码还有两个问题:
1.选择文件窗口选择“取消”程序会出错;
2.显示 ...

这样子可能会看起来更直观一些:
  1. import easygui as g
  2. import sys
  3. file_name = g.fileopenbox(msg = '请选择要阅读的文本文件:',title = '文本阅读程序')
  4. if not file_name:
  5.     sys.exit(0)
  6. with open(file_name,'r') as f:
  7.     file_content = f.read()
  8.     content = g.textbox(msg = '文件【%s】的内容如下:' % file_name,title = '显示文件内容:',text = file_content)
  9. if not content:
  10.     sys.exit(0)
  11. if file_content != content:
  12.     choice = g.buttonbox(msg = '检测到文件内容发生改变,请选择以下操作:',title = '警告',choices = ('覆盖保存','放弃保存','另存为...'))     #<---choices的参数要用括号括起来
  13.     if choice == '覆盖保存':
  14.         f = open(file_name,'w')
  15.         f.writelines(content)
  16.         f.close()
  17. #     elif choice == '放弃保存':   #<---实际上选择这项就是什么都不用做,所以都不用写,选了它就顺其自然退出程序了。
  18. #         return                          #<---return只能用在函数里返回
  19.     elif choice == '另存为...':
  20.         new_location = g.filesavebox()
  21.         f2 = open(new_location,'w')
  22.         f2.writelines(content)
  23.         f2.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-12 17:35:10 | 显示全部楼层
txxcat 发表于 2020-5-12 16:22
实际上,解决了表面的两个问题,你的代码还有两个问题:
1.选择文件窗口选择“取消”程序会出错;
2.显示 ...

真的谢谢大佬!搞明白啦谢谢谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-30 10:59:21 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-20 18:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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