Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Tkinter to create line-wrapped text that fills the width of the window?

Tags:

The Label widget doesn't line-wrap. The Message widget will line-wrap text, but forces it to be roughly square. Here's an example:

from Tkinter import *  root = Tk() root.title("hello")  Message(root, text=48*'xxxxx ').grid(row=0, column=0, columnspan=3)  Label(root, text='Name:').grid(row=1, column=0) Entry(root, width=50).grid(row=1, column=1) Button(root, text="?").grid(row=1, column=2)  Button(root, text="Left").grid(row=2, column=0) Button(root, text="Center").grid(row=2, column=1) Button(root, text="Right").grid(row=2, column=2)  root.mainloop() 

I know that I can use aspect=700 to change the shape, but hard-coding numbers like that is what I'm trying to avoid.

like image 245
samwyse Avatar asked Aug 14 '12 09:08

samwyse


2 Answers

The Tkinter Label widget does wrap. It is just that the default setting is no wrapping. To get the text on one to wrap set the wraplength parameter, the units for this are screen units so try wraplength=50 and adjust as necessary. You will also need to set justify to LEFT, RIGHT, or CENTER.

like image 143
Jim Denney Avatar answered Oct 31 '22 18:10

Jim Denney


welcomenote = Label(root, text="Your long text", font="helvetica 14",  wraplength=300, justify="center") welcomenote.pack() 
like image 32
Uduak Akpan Avatar answered Oct 31 '22 20:10

Uduak Akpan