|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
写的时候又发现了自己很多的问题,一边解决一边写,很有收获
- #构建一个输入并可以选择保存文件地址的界面
- #引入模块
- import easygui as e
- import os
- #准备进入程序
- msg = '欢迎进入文件编辑界面'
- title = '文件界面'
- ok_button = '点击进入'
- e.msgbox(msg,title,ok_button)
- #因为不知道怎么在界面中进行文本编辑,就只能先建立空的文本,再通过textbox写入
- #开始弄
- #先想办法得到文件名
- file_name = e.enterbox(title='文件界面',msg='请输入要编写文件的文件名(默认保存格式为txt)')
- file_name = file_name + '.txt'
- #然后以写入模式打开文件
- with open(file_name,'w+') as order_file :
- e.msgbox(msg='现在开始编辑内容',ok_button='点击进入',title='文件界面')
- text = order_file.read()
- text_content = e.textbox(msg='正在编辑内容...',title='文件界面',text=text) #得到内容
- order_file.write(text_content[ :-1]) #写入之前建好的空文本中
- while 1: # 考虑到万一用户想要修改
- e.msgbox(title='文件界面',msg='文件编辑完成',ok_button='点击查看')
- with open(file_name,'rt') as scan_file:
- msg='刚才编辑的文件【%s】内容如下:'% file_name
- text = scan_file.read()
- e.textbox(msg,title='文件界面',text=text)
- user_choice = e.choicebox(title='文件界面',msg='需要重新修改文件吗?',choices=('是的','不用了'))
- if user_choice =='不用了':
- break
- else:
- with open(file_name,'r+') as order_file:
- text = order_file.read()
- text_content = e.textbox(title='文件界面',msg='正在重新编辑中...',text=text)
- with open(file_name,'w') as order_file:
- order_file.write(text_content[:-1])
-
- choice= e.choicebox(title='文件界面',msg='之前只能保存在桌面哦^_^\ndo you want to try again?',choices=('你这....好吧,我存!','nmb...耍我呢,不存了'))
- if choice == '你这....好吧,我存!' :
- file_path = e.filesavebox(default=file_name)
- f=open(file_path,'w+')
- f.write(text_content[:-1])
- f.close()
复制代码 |
|