Skip to content Skip to sidebar Skip to footer

How Can I Display A Long String (paragraphs) In A Label Widget On Multiple Lines With A Y Scrollbar? (tkinter)

Hey im trying to put text that contains a few paragraphs in a label widget. I would like it to display on multiple lines and use a y scrollbar so everything can fit and be viewed i

Solution 1:

The solution is to copy and paste your text in another with the state disabled.

To make the widget read-only, you can change the state option from NORMAL to DISABLED:

     text.config(state=NORMAL)
     text.delete(1.0, END)
     text.insert(END, text)
     text.config(state=DISABLED)

Note that you must change the state back to NORMAL before you can modify the widget contents from within the program. Otherwise, calls to insert and delete will be silently ignored.

more

to show you an executable example:

import tkinter as tk

root = tk.Tk()

def show_it(event):
    text = write_me.get('1.0','end') #get the text
    write_me.delete('1.0','end') #delet the text
    write_me.index('insert')
    
    show_me.config(state='normal')
    show_me.insert('end', text)
    show_me.config(state='disabled')

write_me = tk.Text(root)
write_me.pack(side='left')
write_me.bind('<Return>', show_it)

show_me = tk.Text(root,state='disabled')
show_me.pack(side='right')

root.mailoop()

Also note you can do this to simplify it some more:

show_me = tkst.ScrolledText(root,state='disabled') #if you have python 3.7+

Post a Comment for "How Can I Display A Long String (paragraphs) In A Label Widget On Multiple Lines With A Y Scrollbar? (tkinter)"