Label Tkinter several lines

2

Hello, I have a problem in my application when using labels. The problem is that when writing a long text, the text continues and leaves the window. What I want is that the text does not pass from the area delimited by the window, and is put as a paragraph in several lines in the space that is available. I leave an example that simulates what I am telling in case I have not explained myself well.

import tkinter as tk
from tkinter import ttk


class Application(ttk.Frame):
    def __init__(self, main_window):
        super().__init__(main_window)
        main_window.geometry("400x500")
        self.text = tk.StringVar(value="Esto es un texto largo de ejemplo 
        para Stackoverflow. Esto es un texto largo de ejemplo para 
        Stackoverflow.")

        self.label = tk.Label(self, text="text:").grid(column=0, row=0, 
        pady=10, padx=10, sticky="e")
        self.label2 = tk.Label(self, text=self.text.get()).grid(column=1, 
        row=0, pady=20, sticky="w")




if __name__ == "__main__":
    root = tk.Tk()
    app = Application(root)
    app.pack(expand=True, fill='both')
    root.mainloop()
    
asked by Alfredo Lopez Rodes 10.09.2018 в 17:24
source

1 answer

0

tkinter.Label has a parameter to define when you must break a text that exceeds a certain size in another line, which is wraplength .

self.label2 = tk.Label(self, textvariable=self.text, wraplength=390)

Keep in mind that receives the length from which to break the string in pixels, not the number of characters . This is so to allow the code to be portable to any platform and source, but it is difficult to assign a value dynamically when we want the text to be properly divided when resizing the window or the widget automatically. To allow this, what we must do is modify the parameter once the widget is rendered, an example:

import tkinter as tk
from tkinter import ttk



class Application(ttk.Frame):
    def __init__(self, main_window):
        super().__init__(main_window)
        main_window.geometry("400x500")
        self.text = tk.StringVar(value=("Esto es un texto largo de ejemplo "
                                        "para Stackoverflow."
                                        "Esto es un texto largo de ejemplo "
                                        "para Stackoverflow."))

        self.label = tk.Label(self, text="text:")
        self.label.grid(column=0, row=0, pady=10, padx=10, sticky="e")
        self.label2 = tk.Label(self, textvariable=self.text)
        self.grid_columnconfigure(1, weight=1)
        self.label2.grid(column=1, row=0, pady=20, padx=10,  sticky="nswe")
        self.label2.bind( "<Configure>", self.on_label_resize)

    def on_label_resize(self,  event):
        event.widget["wraplength"] = event.width


if __name__ == "__main__":
    root = tk.Tk()
    app = Application(root)
    app.pack(expand=True, fill='both')
    root.mainloop()

Two observations:

  • If you assign the text to Label from a StringVar use the parameter textvariable no text , this makes that if the variable is modified the Label is automatically modified. If you do not want this to happen and the text assignment is static then it is correct to use text=string_var.get()

  • Do not use grid , pack or place on the same line where instances unless you do not want a reference to the widget to use it later, if you do

    self.label = tk.Label(self, text="text:").grid(...)
    

    The label attribute will refer to the output of the grid ( None ), not the Label instance, so it is useless in the future to refer to the widget.

answered by 10.09.2018 / 19:01
source