1

When I go to open a file I get the aforementioned error code "AttributeError: 'str' object has no attribute 'read'"

Any idea how to fix this? In advance I appreciate any help

Here is my code below:

import tkinter.scrolledtext as ScrolledText
from tkinter import *

root = Tk()
root.title("Diary")
root.minsize(width=400, height=400)
root.maxsize(width=800, height=480)

text= ScrolledText.ScrolledText (root, width=400, height=400)
text.pack()

def donothing():
   x = 0

def newFile():
    global filename
    filename = "Untitled"
    text.delete(0.0, END)

def openFile():
    rootFilename =  filedialog.askopenfilename(initialdir = "E:/Images",title = "choose your file",filetypes = (("Text file","*.txt"),("all files","*.*")))

    if rootFilename != None:
       contents= rootFilename.read()
       TextArea.insert('1.0', contents)
       file.close()

def saveFile():
    name = filedialog.asksaveasfile(mode = 'w',filetypes = (("Text file","*.txt"),("all files","*.*")))
    text2save = str(text.get(0.0,END))
    name.write(text2save)
    name.close

def Exit():
        root.destroy()

menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=newFile)
filemenu.add_command(label="Open", command=openFile)
filemenu.add_command(label="Save", command=saveFile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=Exit)
menubar.add_cascade(label="File", menu=filemenu)

root.config(menu=menubar)
root.mainloop()

4
  • 1
    askopenfilename merely returns a string of the file path. You need to open the file yourself. Commented Mar 30, 2019 at 4:36
  • I have an open command, shouldn't that do the trick? If not how would I change my code for this to work? Commented Mar 30, 2019 at 4:38
  • Where is filedialog defined? Commented Mar 30, 2019 at 4:43
  • sent the full code. Its placed in openfile and savefile Commented Mar 30, 2019 at 4:45

1 Answer 1

1

You need to open the file yourself from the return string of askopenfilename.

def openFile():
    rootFilename =  filedialog.askopenfilename(initialdir = "E:/Images",title = "choose your file",filetypes = (("Text file","*.txt"),("all files","*.*")))

    if rootFilename:
        with open(rootFilename,"r") as f:
            f = f.read()
            text.insert('1.0', f)

Or what you wanted might be askopenfile instead:

def openFile():
    rootFilename = filedialog.askopenfile(initialdir="E:/Images", title="choose your file",
                                              filetypes=(("Text file", "*.txt"), ("all files", "*.*")))

    if rootFilename:
        rootFilename = rootFilename.read()
        text.insert('1.0', rootFilename)
Sign up to request clarification or add additional context in comments.

5 Comments

I get this error when I try and open:TextArea.insert('1.0', rootFilename) NameError: name 'TextArea' is not defined
And where did you define TextArea?
dont think i did, where and how would you define it?
It was from your original code. But you already created a Scrolledtext widget with the name text, i assume you will use that instead?
You were asking on why AttributeError: 'str' object has no attribute 'read' and the above is why you are getting the error. To insert it into a widget relies on how you created the widget in the first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.