Skip to content
← Back to Community
I'm have trouble reading the askopenfile and printing it out in a text document. Can you help?
Profile icon
SonjaCrockett

from tkinter import *
from tkinter import ttk
from tkinter.scrolledtext import *
from tkinter.filedialog import asksaveasfile
from tkinter.filedialog import askopenfile
from tkinter.filedialog import askopenfilename
root=Tk()

def save():
files = [('All Files', '.'),('Python Files', '.py'),('Text Document', '.txt')]
file = asksaveasfile(filetypes = files, defaultextension = files)

def open_file():
files = [('All Files', '.'),('Python Files', '.py'),('Text Document', '.txt')]
file_read = askopenfile(mode = 'r')
TextBox = ScrolledText(root, height='10', width='45', wrap=WORD)
count=0
while(count<100):
TextBox.insert(END,file_read)
TextBox.yview(END)
count += 1

TextBox.grid(row=1,column=1)

def openfilename():
filename = askopenfilename()
txtfld = ttk.Label(text=filename).grid(row=0,column=1)

def New_text():
file_read.delete("1.0")

#text and button for filename
txt_filename=ttk.Entry(root, textvariable=askopenfilename)
txt_filename.grid(row=0,column=1)
btn1 = ttk.Button(text ='Filename', command = openfilename)
btn1.grid(row=0,column=0)

#text and label for open file text
Text_text = ttk.Label(root, text= "Enter text")
Text_text.grid(row=1,column=0)
txt_of_file=ttk.Entry(root, textvariable=askopenfile)
txt_of_file.grid(row=1,column=1)

#buttons for open, save, and new
btn2 = ttk.Button(root, text ='Open', command = open_file)
btn2.grid(row=4,column=0)
btn3 = ttk.Button(root, text = 'Save', command = save)
btn3.grid(row=4,column=1)
btn4 = ttk.Button(root, text = 'New ', command = New_text)
btn4.grid(row=4,column=2)

root.mainloop()

Voters
Profile icon
YoCarry
Profile icon
SonjaCrockett