Daveolw1
Posts: 23
Joined: Thu May 16, 2019 10:24 am

tkinter Text Widget putting "{}" around text I insert

Thu May 16, 2019 10:40 am

Using Raspberry pi 3 Python and tkinter GUI and reading from file and inserting in Text Widget, but the text is wrapped by curly brackets and it also shows the text 4 times.
My code:-

Code: Select all

from tkinter import *
root = Tk()
T = Text(root, height=40, width=100)
T.pack()
f = open('/home/pi/Documents/song1.txt', 'r')
f1 = f.readlines()
for x in f1:
    T.insert("1.0", f1)
mainloop()
Output:-
{ABIDE WITH ME, fast falls the eventide;
} {The darkness deepens, Lord, with me abide;
} {When other helpers fail and comforts flee,
} {Help of the helpless, O abide with me.}{ABIDE WITH ME, fast falls the eventide;
} {The darkness deepens, Lord, with me abide;
} {When other helpers fail and comforts flee,
} {Help of the helpless, O abide with me.}{ABIDE WITH ME, fast falls the eventide;
} {The darkness deepens, Lord, with me abide;
} {When other helpers fail and comforts flee,
} {Help of the helpless, O abide with me.}{ABIDE WITH ME, fast falls the eventide;
} {The darkness deepens, Lord, with me abide;
} {When other helpers fail and comforts flee,
} {Help of the helpless, O abide with me.}[
The file song1.txt:-

ABIDE WITH ME, fast falls the eventide;
The darkness deepens, Lord, with me abide;
When other helpers fail and comforts flee,
Help of the helpless, O abide with me.

Why is it putting "{}" around the lines, how do I remove them?
Why is it repeating the 4 lines 4 times?

Thanks for any help David.

Andyroo
Posts: 4211
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: tkinter Text Widget putting "{}" around text I insert

Thu May 16, 2019 12:02 pm

My first thought is the braces are the cr/lf at the end of each line

If you use

Code: Select all

f1 = f.readline()
You will read each line at a time. The you need to use the replace command to strip them out.

As for showing it four times thats due to the for loop and the insert of the wrong item.

You are looping with 'x' in f1 - f1 contains four line so the loop goes around four time BUT you add the full text in to the box by referencing f1 in the insert.

Try the following untested code (no GUI Pi handy as normal):

Code: Select all

from tkinter import *
root = Tk()
T = Text(root, height=40, width=100)
T.pack()

# Note use of tilde to save typing :-)
with open("~/Documents/song1.txt") as f:
    f1 = f.read()
    f1 = f1.strip() # Take out CR / LF from the text read from file
    T.insert("1.0", f1)

# File is automatically closed after the read loop is complete
mainloop()
Just be careful with the case of variable - Python is case sensitive and the 'recommended format' is to use lowercase words separated with underlines
e.g.
ScreenDisplay would be screen_display

I now cannot get Eventide out of my mind - could be worse I suppose :D

edit: Corrected double dots and added comment to strip.
Need Pi spray - these things are breeding in my house...

scotty101
Posts: 3680
Joined: Fri Jun 08, 2012 6:03 pm

Re: tkinter Text Widget putting "{}" around text I insert

Thu May 16, 2019 12:33 pm

Change

Code: Select all

T.insert("1.0", f1)
to

Code: Select all

T.insert("1.0", x)
You are inserting the whole song each time rather than each line.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Daveolw1
Posts: 23
Joined: Thu May 16, 2019 10:24 am

Re: tkinter Text Widget putting "{}" around text I insert

Thu May 16, 2019 8:10 pm

Thanks for the replies.
Andyroo, I copied your code but it brought up an error 2 file not found but replacing the "~" with /home/pi it worked fine.

scotty101 I put the "x" in place of "f1" and it worked but the lines were in reverse order, and 2 lines were in one line.

Thanks for your help I realise where I was going wrong, I did not need a loop.

David

User avatar
Paeryn
Posts: 2636
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: tkinter Text Widget putting "{}" around text I insert

Thu May 16, 2019 9:57 pm

Daveolw1 wrote:
Thu May 16, 2019 8:10 pm
scotty101 I put the "x" in place of "f1" and it worked but the lines were in reverse order, and 2 lines were in one line.
You had the lines in reverse order because you had T.insert("1.0", x) which says "insert x at line 1, column 0", so the the first line was put in to the text widget then the second line was inserted before the first etc. To insert a line at the very end you use T.insert(Tk.END, x)
She who travels light — forgot something.

Daveolw1
Posts: 23
Joined: Thu May 16, 2019 10:24 am

Re: tkinter Text Widget putting "{}" around text I insert

Sat May 18, 2019 9:58 am

Can you explain how this works

Code: Select all

# Note use of tilde to save typing :-)
with open("~/Documents/song1.txt") as f:
    f1 = f.read()
    f1 = f1.strip() # Take out CR / LF from the text read from file
    T.insert("1.0", f1)
If the CR/LF have been stripped out how does the insert know what is a line?
I assumed "f1" was a list off lines, but if I try "f1[2]" you only get the character at that position.

User avatar
Paeryn
Posts: 2636
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: tkinter Text Widget putting "{}" around text I insert

Sat May 18, 2019 1:03 pm

Daveolw1 wrote:
Sat May 18, 2019 9:58 am
Can you explain how this works

Code: Select all

# Note use of tilde to save typing :-)
with open("~/Documents/song1.txt") as f:
    f1 = f.read()
    f1 = f1.strip() # Take out CR / LF from the text read from file
    T.insert("1.0", f1)
If the CR/LF have been stripped out how does the insert know what is a line?
I assumed "f1" was a list off lines, but if I try "f1[2]" you only get the character at that position.
After f1 = f.read() f1 will be one string containing the entire contents of the file.
f1 = f1.strip() strips the leading and trailing whitespace from the string. So the first character will be the first non-whitespace character of the file and the last character will be the last non-whitespace character of the file, every character (both whitespace and non-whitespace) between those two will be kept as-is.
E.g. <LF> being the line feed character

Code: Select all

<LF>Hello<LF>There<LF>
will end up as

Code: Select all

Hello<LF>There
She who travels light — forgot something.

Return to “Python”