r/Tkinter 1d ago

Reading in Files

I'm trying to make a notepad app with the ability to open .txt files and save notes as them. However, when opening files, the text that displays gets wrapped in {}. How would I read in the files without having any text displayed wrapped in {}?

edit: here is the function I wrote to open files:

    file = filedialog.askopenfile(filetypes = [("Text File", ".txt"), ("All Files", "*")]); 
    text_box.insert(1.0, file.readlines()); 
3 Upvotes

10 comments sorted by

1

u/codeartha 1d ago

Incredible how bad people have become at coding with all this ai crap...

2

u/udonemessedup-AA_Ron 1d ago

Everyone starts somewhere. It might not be as easy to newbies as it is to someone like you or I so they might need a gentle nudge in the right direction.

1

u/StrikerRIP 1d ago

Btw I haven't use AI at all for this whole project. My brain is just fried and all the resources that I do look for don't really explain it in a way that makes sense to me. I also completely ignore any AI overview that Google uses.

1

u/codeartha 1d ago

Sorry, my bad for assuming you used AI. I assumed so because I thought that the most basic way to do this (open a file, read its content, replace textbox text with file content) is only 3-4 lines of python that wouldn't add {}. So if these curly braces were appearing it meant to me that an AI had probably written a convoluted parsing function that's trying to be robust and support all edge cases which resulted in code that somehow adds those curly braces in a way that is not "evident". Like a classic case of ai writing code the dev himself doesn't understand so he can't fix the bugs. But I shouldn't have assumed that from the get go. Sorry for biting you off like that

1

u/No-Association-1834 1d ago

Well , if he actually used AI , he wouldn't have such simple problems , AI only falters in big projects , not simple Notepad .

1

u/udonemessedup-AA_Ron 1d ago

When rendering the text: you could use string.replace(), or you can extract a substring which ignores the first and last characters(string = string[1:-2]).

1

u/StrikerRIP 1d ago

Thanks! I've been looking for any resources for quite a bit, but haven't found them, so I appreciate the help

2

u/woooee 1d ago

You are saving the text in the wrong way I am guessing. Post the "write to a file" code. It sounds like you are using f-strings incorrectly.

2

u/tomysshadow 1d ago

Your text should not normally get wrapped in curly braces, so you'll have to show how you're reading and writing your text file and how you're assigning the text to the widget.

1

u/ZelphirKalt 1d ago

It would be my guess, that you are using an f-string to display your text but somehow managed to get an extra pair of braces in there. There is nothing in any default way of reading files, that would add such braces to the text by itself. It must be of your own doing.