Answers for "how to read and write to text files in tkinter"

0

how to read and write to text files in tkinter

from tkinter import *
from tkinter import ttk
from tkinter import filedialog



root=Tk()
root.title("Get Height and Width")

# width=400, height=400
root.geometry("400x500")


def clear():
    my_text.delete(1.0,END)

#Grab the Text from the Text box
def open_txt():
    text_file=filedialog.askopenfilename(initialdir="D:/IT related folders and documents/",title="Open Text File",filetypes=(("Text Files", "*.txt"),))
    text_file=open(text_file,'r')
    stuff=text_file.read()
    my_text.insert(END,stuff)
    text_file.close()

#Save Text File
def save_txt():
    text_file=filedialog.askopenfilename(initialdir="D:/IT related folders and documents/",title="Save Text File",filetypes=(("Text Files", "*.txt"),))
    text_file=open(text_file,'w')
    text_file.write(my_text.get(1.0,END))
    text_file.close()

my_text=Text(root,width=60,height=20)
my_text.pack(pady=20)

open_button=Button(root,text="Open Text File",command=open_txt)
open_button.pack(pady=20)

save_button=Button(root,text="Save Text File",command=save_txt)
save_button.pack(pady=20)

root.mainloop()
Posted by: Guest on September-07-2021

Code answers related to "how to read and write to text files in tkinter"

Python Answers by Framework

Browse Popular Code Answers by Language