场景是我想:
目前我已经研究了如何将特定的文本文件打开到文本小部件中并显示它但是我无法弄清楚如何进行最后一步。
我试图定义一个' Save'但是功能却没有,你可以在下面看到。
步骤1和2的当前代码:
function Main($scope, $document) {
$scope.sample = function(myvalue) {
var e = $document[0].getElementById('yo')
.querySelector('[value="' + myvalue + '"]');
// Get Text option selected
console.log('Text: '+angular.element(e).text()); // Text
console.log('Value: '+myvalue) // Value
}
}
如果有人能在这里帮助我,那就太棒了
干杯球员
答案 0 :(得分:0)
一旦你知道Widget indices是如何工作的,你知道insert
和get
methods on the Text
widget:
starting_text = "THIS WOULD COME FROM A FILE"
...
textbox = TEXT_WIDGET_SETUP_ALREADY
textbox.insert("1.0",starting_text)
...
ending_text = textbox.get("1.0","end-1c")
棘手的部分是在程序关闭时访问文本但是在窗口小部件被销毁之后(或者你得到_tkinter.TclError: invalid command name ".4384096888"
错误)访问文本:
import tkinter as tk
class Text(tk.Text):
def destroy(self):
global ending_text
ending_text = self.get("1.0","end-1c")
super(Text,self).destroy()
虽然如果您使用from tkinter import *
表示法,则需要将您的课程称为Text
以外的其他内容,并且可能不使用ending_text
作为全局变量,但这是最简单的方法展示如何做到这一点。
这是我用于测试IO的完整代码,但如果您不了解如何处理文件,则references elsewhere。
import tkinter as tk
filename = "test.txt"
class Text(tk.Text):
def destroy(self):
global ending_text
ending_text = self.get("1.0","end-1c")
super(Text,self).destroy()
try:
with open(filename) as f:
text = f.read()
except IOError:
text = ""
root = tk.Tk()
textbox = Text(root)
textbox.insert("1.0",text)
textbox.grid()
#this would probably just be put in the destroy method
def finish(event=None):
with open(filename,"w") as f:
f.write(ending_text)
textbox.bind("<Destroy>",finish) #this will happen after Text.destroy() so textbox.get() fails if used from this point
root.mainloop()
答案 1 :(得分:0)
我创建了一个简单的用户界面,允许您从文件对话框中打开所选的文本文件。出于可伸缩性的原因,我更喜欢将其后的选项设置为分开(例如,如果您希望将来更喜欢读取文档文件):
# Opening file options
self.file_options={}
self.file_options['defaultextension'] = '.txt'
self.file_options['filetypes'] = [('text files', '.txt'), ('all files', '.*')]
self.file_options['parent'] = self.parent
self.file_options['title'] = 'Open a text file'
self.file_options['initialdir']='/home/'
我正在运行Linux,因此,如果您使用的是MS Windows操作系统,则可以将self.file_options['initialdir']='/home/'
更改为您想要的任何目录路径。请注意,您也可以将其删除,在这种情况下,默认情况下,文件对话框窗口会提示您运行应用程序的目录。
方法initialize_user_interface()
执行其名称反映的内容。主要是,它提供了一种在单击时退出应用程序的舒适方式,并选择要阅读的文件:
self.filemenu.add_command(label="Open",command=self.text_replacement)
self.filemenu.add_command(label="Exit",command=self.parent.quit)
然后,您可以添加“文本”小部件。最好是有一个可滚动的文本区域,以防您偶然发现大型内容文件。
为此,您需要创建滚动条,并将其附加到Text小部件,因为Text小部件不会维护自己的滚动条。
以下是完整的计划:
'''
Created on Feb 25, 2016
@author: begueradj
'''
import Tkinter # Tkinter -> tkinter in Python3
import Tkconstants
import tkFileDialog
class Begueradj(Tkinter.Frame):
""" Get text file content and past it into a scrollable Text widget.
Replace the content of the file with the pre-existing Text widget content.
"""
def __init__(self,parent):
""" Set some class variables:
mainly the file dialog interface options.
"""
Tkinter.Frame.__init__(self,parent)
self.parent=parent
# Opening file options
self.file_options={}
self.file_options['defaultextension'] = '.txt'
self.file_options['filetypes'] = [('text files', '.txt'), ('all files', '.*')]
self.file_options['parent'] = self.parent
self.file_options['title'] = 'Open a text file'
self.file_options['initialdir']='/home/'
self.initialize_user_interface()
def initialize_user_interface(self):
""" Design of the user interface.
It mainly consists of a bar menu and a horizontally & vertically
scrollable Text widget in case the text file to read is large.
"""
self.parent.title("Text replacement")
# Set the bar menu and its items
self.menubar=Tkinter.Menu(self.parent)
self.filemenu=Tkinter.Menu(self.menubar,tearoff=0)
self.filemenu.add_command(label="Open",command=self.text_replacement)
self.filemenu.add_command(label="Exit",command=self.parent.quit)
self.menubar.add_cascade(label="File",menu=self.filemenu)
self.parent.config(menu=self.menubar)
self.parent.grid_rowconfigure(0,weight=1)
self.parent.grid_columnconfigure(0,weight=1)
# Set the horizontal and vertical scrollbars
self.hscrollbar=Tkinter.Scrollbar(self.parent,orient=Tkconstants.HORIZONTAL)
self.hscrollbar.grid(row=1,column=0,sticky=Tkinter.E+Tkinter.W)
self.vscrollbar=Tkinter.Scrollbar(self.parent)
self.vscrollbar.grid(row=0,column=1,sticky=Tkinter.N+Tkinter.S)
# Set the Text widget and make it scrollable
self.text=Tkinter.Text(self.parent,wrap=Tkinter.NONE,bd=0,
xscrollcommand=self.hscrollbar.set,
yscrollcommand=self.vscrollbar.set)
self.text.grid(row=0,column=0,sticky=Tkinter.E+Tkinter.W+Tkinter.S+Tkinter.N)
self.text.insert("1.0","Original text here")
self.hscrollbar.config(command=self.text.xview)
self.vscrollbar.config(command=self.text.yview)
def text_replacement(self):
""" Return the name of a file
opened in read mode
"""
self.filename = tkFileDialog.askopenfilename(**self.file_options)
if self.filename:
self.original=self.text.get("0.0","end-1c")
print self.original
with open(self.filename) as self.filetoread:
self.txtfilecontent=self.filetoread.read()
self.filetoread.close()
self.text.delete("1.0", Tkinter.END) # Erase the previous Text widget content
self.text.insert("1.0", self.txtfilecontent)
with open(self.filename,'w') as self.filetowrite:
self.filetowrite.write(self.original)
self.filetowrite.close()
def main():
""" Main method to be executed.
Instantiate Begueradj class
"""
root=Tkinter.Tk()
b=Begueradj(root)
root.geometry("300x250+300+300")
root.mainloop()
if __name__=="__main__":
""" Run the application
"""
main()
应用演示:
该演示包含3个屏幕截图: