0

I am trying to show the text in tkinter window from a list of string. But problem is string is very long , I want that it shows only 50 character in a line and then rest 50 in next so on for full string.

news_full=['Finance Minister Arun Jaitley said on Friday that the rules for small businesses and exporters would be eased under the Goods and Services Tax (GST), a move that could provide relief to thousands of small firms.', 'Employers may now have more leeway to withhold birth control coverage on religious grounds, according to new rules issued by the US Department of Health and Human Services.', "Samantha Ruth Prabhu and Naga Chaitanya tied the knot on Friday evening. And we need to thank Naga's father and actor Nagarjuna Akkineni for sharing the first pictures of the couple. Here are the latest photos from Samantha Ruth Prabhu and Naga Chaitanya's grand Goa wedding. Scroll on.", 'The storm brought heavy rain to Central America, where more than 20 people died. The storm is headed toward Louisiana, where Gov. John Bel Edwards said, "I\'m not going to tell you I am not concerned."', "A police official said investigators don't think anyone else was in the shooter's room before the Las Vegas attack, but are looking if anyone knew about his plans.", "MELBOURNE, Australia (AP) — Australia cricket captain Steve Smith will return home from the team's tour of India with a right shoulder injury, Cricket Australia said Saturday.", 'Hurricane Nate gained force as it headed toward the central Gulf of Mexico early Saturday after drenching Central America in rain that was blamed for 21 deaths.', 'India, meanwhile, said there were “no new developments” at the face-off site, and that the status quo continues.', 'The possible sale of the advanced system can go ahead if congress does not object within 30 days.', 'Thousands of people gather at 40 locations across the country on Saturday as part of the Stop Adani Alliance']

root=Tk()

for s in news_full:
    texr2=Label(root,text=s,font=("sans-serif",32))
    texr2.pack()

root.geometry('545x800')

root.mainloop()

so expected output is (ex for first element of list]

Finance Minister Arun Jaitley said on Friday that 
the rules for small businesses and exporters would
be eased under the Goods and Services Tax (GST), 
a move that could provide relief to thousands of 
small firms.

same for all element of list.

2 Answers2

1

You can use wraplength when creating a Label object to make the text wrap by itself.

This is given in screen units, so if your window is 545 pixel wide (as in your example), you could pass a wraplength value of 545 such that it wraps within your window.

To have your text aligned to the left as in your expected output, you could pass the argument justify=LEFT, and anchor=W.

Your line where you create a label would therefore look like:

texr2=Label(root, text=s, wraplength=545, anchor=W, justify=LEFT, font=("sans-serif",14))

Note that I changed the font size so all your text fits to the window size.

You also want to ensure everything fills properly by packing with the arguments fill=BOTH and expand=True:

texr2.pack(expand=True, fill=BOTH)
SneakyTurtle
  • 1,831
  • 1
  • 13
  • 23
0

Certainly use TK's automatic wrapping if possible. But if not, you can do the following:

from textwrap import wrap

s = "Some very long string. Or short. It does not matter really. Blah, blah, blah!\nIt can be multilined too!"
print "\n".join(wrap(s, 25))

So this will wrap any text to 25 characters per line. A list of lines will be returned.

Dalen
  • 4,128
  • 1
  • 17
  • 35