Jun-26-2022, 10:59 AM
(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!