Python Forum
[Tkinter] How to wrap text within text box
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to wrap text within text box
#1
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()
Reply
#2
Please show the complete code without attachments.
Generally to wrap the text in a widget I use the following:
widget_name.config(wrap=WORD)
"Often stumped... But never defeated."
Reply
#3
(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).
Reply
#4
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Transparent window background, but not text on it muzicman0 7 2,734 Feb-02-2024, 01:28 AM
Last Post: Joically
  [Kivy] Create a function to store text fields and drop downs selection in KivyMD floxia 0 1,608 Dec-18-2022, 04:34 AM
Last Post: floxia
  [Tkinter] Updating tkinter text BliepMonster 5 5,658 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  Can't change the colour of Tk button text Pilover 6 14,494 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] QLineEdit Caret (Text Cursor) Transparency malonn 5 2,730 Nov-04-2022, 09:04 PM
Last Post: malonn
  [PyQt] Hover over highlighted text and open popup window DrakeSoft 2 1,446 Oct-29-2022, 04:30 PM
Last Post: DrakeSoft
  [PyQt] Determine whether text in QTableView cell is fully visible or not random_nick 0 952 Oct-27-2022, 09:29 PM
Last Post: random_nick
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,757 Aug-23-2022, 09:11 PM
Last Post: Extra
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,665 Jun-26-2022, 06:26 PM
Last Post: menator01
  [PyQt] Determining the format attributes on text in a QTextEdit object DrakeSoft 7 3,447 Apr-18-2022, 06:40 PM
Last Post: DrakeSoft

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020