Search code examples
pythontkintertextline-breaksfill

How to get rid of line breaks and fill box with text/text wrap in python GUI


I need some help with this project. I cannot find the command to wrap text (fill box without line breaks) that I input into the widget. What I want to do get rid of line breaks when I push the 'wrap' button. What I want to do is fill the box with text and get rid of the line breaks and wrap the text within the box.

from tkinter import *

from tkinter import scrolledtext
import textwrap

window = Tk()

window.title("Welcome to Text Wrap 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()

I'm stuck on btn2.


Solution

  • You can try this:

    def doit():
        data = txt.get('1.0', END).replace('\n', ' ')
        txt.delete('1.0', END)
        txt.insert(INSERT, data)
    
    btn2 = Button(window,text='Wrap', command=lambda: doit())