Python Forum

Full Version: How to wrap text within text box
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
Please show the complete code without attachments.
Generally to wrap the text in a widget I use the following:
widget_name.config(wrap=WORD)
(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).
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()