Python Forum
[Tkinter] How to wrap text within text box - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] How to wrap text within text box (/thread-26847.html)



How to wrap text within text box - pav1983 - May-15-2020

Hi all,

I need some help with this project. I cannot find the command to wrap text that I've already imputted into the widget. What I want to do get rid of line breaks when I push the 'wrap' button.

Here is my code so far.

from tkinter import *

from tkinter import scrolledtext
import textwrap

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('500x500')

txt = scrolledtext.ScrolledText(window,width=50,height=20)

txt.grid(column=0,row=0)

btn1 = Button(window,text='Clear', command=lambda: txt.delete(1.0,END))
btn2 = Button(window,text='Wrap', command=lambda: textwrap)

btn1.grid(column=4, row=15)
btn2.grid(column=8, row=15)



window.mainloop()



RE: How to wrap text within text box - DT2000 - May-15-2020

Please show the complete code without attachments.
Generally to wrap the text in a widget I use the following:
widget_name.config(wrap=WORD)



RE: How to wrap text within text box - pav1983 - May-16-2020

(May-15-2020, 08:16 PM)DT2000 Wrote: Please show the complete code without attachments.
Generally to wrap the text in a widget I use the following:
widget_name.config(wrap=WORD)

It is the complete code. It's just the wrapping part I'm stuck on (line 17). The attachment is the exact same thing as the code, so I took it down.

Just for clarity, I am looking to cut and paste a text with broken lines into the program and then fill the box with the text (eliminate the line breaks).


RE: How to wrap text within text box - pav1983 - May-17-2020

Just for anyone who's interested, here is the solution:

from tkinter import *

from tkinter import scrolledtext
import textwrap

window = Tk()

window.title("Text Wrap Tool")

window.geometry('600x500')

txt = scrolledtext.ScrolledText(window,width=50,height=20)

def doit():
    data = txt.get(1.0, END).replace('\n', ' ')
    txt.delete(1.0, END)
    txt.insert(INSERT, data)

t = Text()
t.config(wrap=WORD)

txt.grid(column=0,row=0)

btn1 = Button(window,text='Clear', command=lambda: txt.delete(1.0,END))
btn2 = Button(window,text='Wrap', command=lambda: doit())

btn1.grid(column=4, row=15)
btn2.grid(column=8, row=15)

window.mainloop()