Home > other >  The Label widget Tkinter cuts off the Long text in the view
The Label widget Tkinter cuts off the Long text in the view

Time:06-27

I'm using Windows 10 and Anaconda Jupyter Python 3.9.12. 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")




df = "Mittagessen: Sie ordern ein Steak, englisch. Der Kellner bringt es durchgebraten. Was tun Sie?"


def random():
    global l2
    global l1
    
    df1 = df
    
    Q = df1
   
    l2.config(text = Q)


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

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



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

l2 = tk.Label(window, bg="#F73C12",font=("Arial",15), text= "", width=100, justify=CENTER, compound=tk.CENTER)



l2.bind("<Configure>", random)

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




window.mainloop()

Edit: I have put a direct text in the code - df - instead of the Excel file path.

enter image description here

CodePudding user response:

I don't know if this resolves it but I see few elements which can make problem.

You use width=100 which can create label longer than window but resizable(width=False,height=False) doesn't allow to resize it to show full label.

But I think main problem it because you get row from DataFrame and you send it directly to widget so DataFrame converts it to string adding spaces and using ... to create nice looking table in text console. But this table doesn't have to look nice in GUI. You should get only question from row or format all data in row on your own.


Minimal working code with some data in DataFrame and I use df.sample().values[0][1] to get only question without header and row number. Or df.sample().values[0] to get row and convert it to string manually.

And later I can use wrap() or own method to split to many lines.

I use pack(fill="x") instead of grid() because it is simpler to resize to full window.

import tkinter as tk
import random
import textwrap
import pandas as pd

# --- functions ---

def get_random():
    #Q = df[['type', 'question']].sample().values[0][1]
    #print('Q:', Q)

    row = df[['type', 'question']].sample().values[0]
    print('row:', row)
    Q = '\n'.join(row)
    print('Q:', Q)
    
    # - wrap on length -
    #Q = textwrap.wrap(Q, width=70)
    
    # - wrap on . and : -
    Q = Q.replace('. ', '.\n')#.replace(': ', ':\n')
    
    print('Q:', Q)
    print('---')
    
    question.config(text=Q)

# --- main ---

df = pd.DataFrame({
    'type': [
        'Mittagessen:',
        'Short:',
        'Frage:',
    ],
    'question': [
        'Sie ordern ein Steak, englisch. Der Kellner bringt es durchgebraten. Was tun Sie?',
        'Sentence 1. Sentence 2. Question?',
        'Wie wichtig ist Ihnen unternehmerisches Denken'
    ],
    'answer': [
        'A',
        'B',
        'C',
    ]
})    

# - gui -

window = tk.Tk()

window.title("Welcome to your Q App")
window.geometry('900x500')

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

btn = tk.Button(window, text="Pick up a question", bg="blue", fg="black", command=get_random)
btn.pack()

label = tk.Label(window, text="Please answer", font=("Arial",15), bg="black", fg="white")
label.pack()
         
question = tk.Label(window, bg="#F73C12", font=("Arial",15))
question.pack(fill='x')#, padx=25)
         
get_random()         

window.mainloop()

enter image description here

  • Related