how to read and write to text files in tkinter

Solutions on MaxInterview for how to read and write to text files in tkinter by the best coders in the world

showing results for - "how to read and write to text files in tkinter"
Alberto
01 Jan 2018
1from tkinter import *
2from tkinter import ttk
3from tkinter import filedialog
4
5
6
7root=Tk()
8root.title("Get Height and Width")
9
10# width=400, height=400
11root.geometry("400x500")
12
13
14def clear():
15    my_text.delete(1.0,END)
16
17#Grab the Text from the Text box
18def open_txt():
19    text_file=filedialog.askopenfilename(initialdir="D:/IT related folders and documents/",title="Open Text File",filetypes=(("Text Files", "*.txt"),))
20    text_file=open(text_file,'r')
21    stuff=text_file.read()
22    my_text.insert(END,stuff)
23    text_file.close()
24
25#Save Text File
26def save_txt():
27    text_file=filedialog.askopenfilename(initialdir="D:/IT related folders and documents/",title="Save Text File",filetypes=(("Text Files", "*.txt"),))
28    text_file=open(text_file,'w')
29    text_file.write(my_text.get(1.0,END))
30    text_file.close()
31
32my_text=Text(root,width=60,height=20)
33my_text.pack(pady=20)
34
35open_button=Button(root,text="Open Text File",command=open_txt)
36open_button.pack(pady=20)
37
38save_button=Button(root,text="Save Text File",command=save_txt)
39save_button.pack(pady=20)
40
41root.mainloop()