Python Forum
[Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view
#4
(Jun-26-2022, 09:51 AM)menator01 Wrote: One way of doing it
import tkinter as tk
from random import choice

def random_text(label):
    mylist = [
    'This is a long string of text that maybe will wrap in the label down below. It may have two or three lines of text. Just another example here.',
    'A short line of text here.',
    'One more go with a long line of text. Repeating the long line from above. Or maybe not. Who knows?',
    'Sometimes it seems the label text doesn\'t change. That\'s because it just chooses the same text.',
    'Change the label background color to know the label is changing.'
    ]

    label['text'] = choice(mylist)
    color = []
    for i in range(6):
        color.append(choice('abcdef0123456789'))
    label['bg'] = f'#{"".join(color)}'


root = tk.Tk()
root.geometry('900x500')
root.columnconfigure(0, weight=1)

container = tk.Frame(root)
container.grid(column=0, row=0, sticky='new')
container.grid_columnconfigure(0, weight=3)

label = tk.Label(container, text='Start text here', justify='left')
label['font'] = (None, 26, 'normal')
label.grid(column=0, row=0, sticky='new')

btn = tk.Button(container, text='Random Text')
btn['font'] = (None, 14, 'normal')
btn['command'] = lambda: random_text(label)
btn.grid(column=0, row=1, pady=10)
root.bind('<Configure>', lambda event: label.configure(wraplength=label.winfo_width()))
root.mainloop()

Thank you very much for your code. I tried it and it works perfectly. But could you please tell me what is wrong in my code that I cannot get the whole text in two lines? I would appreciate it if you can!
Reply


Messages In This Thread
RE: The Text in the Label widget Tkinter cuts off the Long text in the view - by malmustafa - Jun-26-2022, 10:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Button to +1 in text box everytime it's clicked martyloo 1 314 May-01-2024, 02:32 PM
Last Post: Axel_Erfurt
  Tkinter: An image and label are not appearing. emont 7 825 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  Transparent window background, but not text on it muzicman0 7 3,005 Feb-02-2024, 01:28 AM
Last Post: Joically
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 927 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,630 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  [Kivy] Create a function to store text fields and drop downs selection in KivyMD floxia 0 1,709 Dec-18-2022, 04:34 AM
Last Post: floxia
  [Tkinter] Updating tkinter text BliepMonster 5 6,265 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  Can't change the colour of Tk button text Pilover 6 14,969 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] QLineEdit Caret (Text Cursor) Transparency malonn 5 2,921 Nov-04-2022, 09:04 PM
Last Post: malonn
  [PyQt] Hover over highlighted text and open popup window DrakeSoft 2 1,583 Oct-29-2022, 04:30 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