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
#1
Hi all,

I'm using Windows 10 and Anaconda Jupyter. I'm trying to build a GUI with one button that randomly picks up a question from an Excel file each time I click on the button. It works fine. But when the question is too long, it just cuts off as in the attached screenshot. I want the line to break, so I can read the whole question and answer it. I tried the wraplength parameter, but it doesn't give me the expected output.

Here is the code:
import tkinter as tk
from tkinter import *
from tkinter import WORD
import pandas as pd
import random

from textwrap import wrap

window = Tk()

window.title("Welcome to your Q App")

window.geometry('900x500')

window.config(bg="#F39C12")
window.resizable(width=False,height=False)




lbl = Label(window, text="Q")

lbl.grid(column=0, row=0)


df = pd.read_excel (r'C:\Users\malmu\Desktop\GUI App\HR_Fragen.xlsx')

def random():
    global l2
    
    df1 = df.sample()  
    
    question = df.sample()
   
    l2.config(text = question)


btn = Button(window, text="Pick up a question", bg="blue", fg="black", command= random)

btn.grid(column=1, row=0)

l1 = tk.Label(text="Please answer",font=("Arial",15),bg="Black",fg="White")

l2 = tk.Label(window, bg="#F73C12",font=("Arial",10),text= "", width=100, justify=LEFT, wraplength=300)

#l2.bind('<Configure>', lambda e: label.config(wraplength=label.winfo_width()))

l2.grid(column=1, row=5)
l1.grid(column=1, row=4)



window.mainloop()
Here how it looks like in the GUI
   


I'm new to the forum, so if my question is not clear or it is not formatted in the way you expect, please let me know.
Many thanks in advance!
Reply
#2
I would consider using the Text widget for this application. The Label widget is considered to be for displaying screen text and while it (the text within) can be modified, it's not quite as simple as in your code.

Have a read of this website, which covers this quite well.

https://tkdocs.com/tutorial/widgets.html#label

edit: you're also missing the 'container' for l1.

l1 = tk.Label(text="Please answer",font=("Arial",15),bg="Black",fg="White")
... corrected

l1 = tk.Label(window, text="Please answer",font=("Arial",15),bg="Black",fg="White")
malmustafa likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
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()
rob101, malmustafa, BashBedlam like this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#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
#5
Compare my code with yours. The bind part is needed as well. You are binding the label, I'm binding to root.
On a side note you have imported random and then redefined it in your code. Just my opinion but, that's bad practice. Also in my opinion it's bad practice to use globals.
malmustafa likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 399 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  Transparent window background, but not text on it muzicman0 7 2,734 Feb-02-2024, 01:28 AM
Last Post: Joically
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 786 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,479 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,608 Dec-18-2022, 04:34 AM
Last Post: floxia
  [Tkinter] Updating tkinter text BliepMonster 5 5,661 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  Can't change the colour of Tk button text Pilover 6 14,496 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,448 Oct-29-2022, 04:30 PM
Last Post: DrakeSoft
  [PyQt] Determine whether text in QTableView cell is fully visible or not random_nick 0 955 Oct-27-2022, 09:29 PM
Last Post: random_nick

Forum Jump:

User Panel Messages

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