在这种情况下,我想:
目前,我已经找到了如何打开文本小部件中的特定文本文件,并使其显示出来,但是我无法确定如何执行最后一步。在
我试图定义一个'Save'函数,但是没有得到任何结果,您可以在下面看到这个。在
步骤1和2的当前代码:
class PropertynewsA(Propertynews):
def __init__(self):
Propertynews.__init__(self)
def save():
file.write(txt)
file.close()
messagebox.showinfo('Saved!','Your Message has been Saved')
self.delete
file = open('PropertynewsA.txt', 'r+') #Text file i am using
with open('PropertynewsA.txt') as file: # Use file to refer to the file object
data = file.read() #date=current text in text file
#Text widget
Time = strftime("%d %b %Y\n")#getting gmt time
txt = Text(self.GenericGui,height=14, width=53,font=('TkDefaultFont',12)) #Creating text widget
txt.insert('1.0',data) #showing text from text file in widget
txt.tag_configure('format', background='lightblue', font='helvetica 14 bold', relief='raised')
txt.focus()
txt.place(x=8,y=40) #places text widget
如果有人能在这里帮助我那就太好了
干杯伙计们
我创建了一个简单的用户界面,允许您从文件对话框打开您选择的文本文件。出于可伸缩性的原因,我希望稍后将此选项分开设置(以防将来您希望阅读文档文件,例如):
我运行的是Linux,所以如果您在MS Windows操作系统上,您可以将
self.file_options['initialdir']='/home/'
更改为您想要的任何目录路径。注意,您也可以删除它,在这种情况下,默认情况下,文件对话框窗口将提示您到运行应用程序的目录。在方法
^{pr2}$initialize_user_interface()
执行其名称所反映的内容。主要是,它提供了一种方便的方法,可以单击退出应用程序并选择要读取的文件:然后可以添加一个文本小部件。最好是有一个可滚动的文本区域,以防您偶然发现一个大的内容文件。在
为此,您需要创建滚动条,并将其附加到文本小部件,因为文本小部件不维护自己的滚动条。在
这是完整的程序:
应用程序演示:
演示包括3个屏幕截图,显示:
file1.txt
的内容加载到Tkinter.Text
小部件中检查文本小部件的原始文件是否保存(替换)在
file1.txt
一旦您知道了Widget indices的工作原理,并且知道了}methods on the ^{} widget :
insert
和{棘手的部分是在程序关闭时访问文本,而不是在小部件被销毁之后(或者您得到一个
^{pr2}$_tkinter.TclError: invalid command name ".4384096888"
错误):虽然如果使用
from tkinter import *
表示法,则需要将类称为Text
之外的名称,也可能不使用ending_text
作为全局变量,但这是演示如何操作的最简单方法。在这是我用来测试IO的完整代码,不过如果您不了解如何处理已经存在的文件,references elsewhere。在
相关问题 更多 >
编程相关推荐