Tkinter Store Text Control Content
Once you have finished arranging the files using the editing program, the next step is to store the arranged files, which will be the focus of this section.
Example#1
a simple document storage program, the program in the File menu contains only two functions: Save and Exit. save can be edited to store the document in apidemos.txt
, Exit is to end this program. When the program is executed, the window title is apidemos.com
, when the document is stored the window title will be changed to the stored document name apidemos.txt
.
from tkinter import *
def saveFile():
textContent = text.get("1.0",END)
filename = "apidemos.txt"
with open(filename,"w") as output:
output.write(textContent)
root.title(filename)
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
menubar = Menu(root) # Create top-level menu
# Create a menu category object and name this menu File
filemenu = Menu(menubar,tearoff=False)
menubar.add_cascade(label="File",menu=filemenu)
# Create a menu list within the File menu
filemenu.add_command(label="Save",command=saveFile)
filemenu.add_command(label="Exit",command=root.destroy)
root.config(menu=menubar)
# Create Text
text = Text(root,undo=True)
text.pack(fill=BOTH,expand=True)
text.insert(END,"Five Hundred Miles\n") # Set Tag at the same time when inserting
text.insert(END,"If you miss the rain I am on,\n")
text.insert(END,"You will knw that I am gone.\n")
text.insert(END,"You can hear the whistle blw\n")
text.insert(END,"A hunded miles,\n")
root.mainloop()
Output:
After you click Save:
Here is the content of apidemos.txt
:
Five Hundred Miles
If you miss the rain I am on,
You will knw that I am gone.
You can hear the whistle blw
A hunded miles,
Although the above procedures can be performed to store documents, but not the GUI design, in the GUI design should be to start the "Save As" dialog box, and then you can choose to store the document folder and then enter the file name. In tkinter.f i ledialog
module has asksaveasf i lename()
method, we can use this method, so that the window dialog box appears, and then perform the storage work.
filename = asksaveasfilename()
The above program can pass back the path (with folders) of the saved documents.
Example#2
Create a File menu, there is a Save As command in this menu, the implementation of this command can appear "Save as" dialog box, then you can select the folder and enter the file name, and finally store the document.
from tkinter import *
from tkinter.filedialog import asksaveasfilename
def saveAsFile():
global filename
textContent = text.get("1.0",END)
filename = asksaveasfilename()
print("The file path passed back is : ",filename)
if filename == "":
return
with open(filename,"w") as output:
output.write(textContent)
root.title(filename)
filename = "apidemos"
root = Tk()
root.title(filename)
root.geometry("300x180")
menubar = Menu(root) # Create top-level menu
# Create a menu category object and name this menu File
filemenu = Menu(menubar,tearoff=False)
menubar.add_cascade(label="File",menu=filemenu)
# Create a menu list within the File menu
filemenu.add_command(label="Save As",command=saveAsFile)
filemenu.add_separator()
filemenu.add_command(label="Exit",command=root.destroy)
root.config(menu=menubar)
# Create Text
text = Text(root,undo=True)
text.pack(fill=BOTH,expand=True)
text.insert(END,"Five Hundred Miles\n") # Set Tag at the same time when inserting
text.insert(END,"If you miss the rain I am on,\n")
text.insert(END,"You will knw that I am gone.\n")
text.insert(END,"You can hear the whistle blw\n")
text.insert(END,"A hunded miles,\n")
root.mainloop()
Output:
The file name used by the author is apidemos2.txt
. When you click the "Save" button, you can save this document and then see the following results in the window.
In fact, in the formal text editing program, there are many things to consider, for example, there can be Save command, you can directly use the current file name to store the document, if not yet saved before the "Save As" dialog box. In addition, you must also consider the use of shortcut keys.
The above-mentioned use of the simplest asksaveasfilename()
method, in fact, there are many parameters within this method can be used, for example, in the previous implementation of the results, you must enter the file name field in the Save New File dialog box contains the extension of the file name, if we feel that the file name entered is a txt
document, you can refer to the following example to set the parameters.
Example#3
Assume that the document created is a txt document.
from tkinter import *
from tkinter.filedialog import asksaveasfilename
def saveAsFile():
global filename
textContent = text.get("1.0",END)
filename = asksaveasfilename(defaultextension=".txt")
print("The file path passed back is : ",filename)
if filename == "":
return
with open(filename,"w") as output:
output.write(textContent)
root.title(filename)
filename = "apidemos"
root = Tk()
root.title(filename)
root.geometry("300x180")
menubar = Menu(root) # Create top-level menu
# Create a menu category object and name this menu File
filemenu = Menu(menubar,tearoff=False)
menubar.add_cascade(label="File",menu=filemenu)
# Create a menu list within the File menu
filemenu.add_command(label="Save As",command=saveAsFile)
filemenu.add_separator()
filemenu.add_command(label="Exit",command=root.destroy)
root.config(menu=menubar)
# Create Text
text = Text(root,undo=True)
text.pack(fill=BOTH,expand=True)
text.insert(END,"Five Hundred Miles\n") # Set Tag at the same time when inserting
text.insert(END,"If you miss the rain I am on,\n")
text.insert(END,"You will knw that I am gone.\n")
text.insert(END,"You can hear the whistle blw\n")
text.insert(END,"A hunded miles,\n")
root.mainloop()
Output:
When saving as, we only entered abc and it was automatically saved as abc.txt.