Python Forum

Full Version: Lock Text Widget height
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everybody,

I've searched for hours and I can't manage to find something, I hope that one of you may help me...

What I'm trying to do is to limit the height of a text box widget in Tkinter. Because for the moment with my code, I can set the height of the text box (25 lines for example). But when I type my text into the text box and I reach the line 25, if I keep on writing, it keeps creating new lines of text. What I would want is to lock the height so the user can just type 25 lines of text and cannot write infinitely.

Here is the GUI Part of my program :

import tkinter as tk
from tkinter import ttk

fenetre = Tk()
fenetre.title("test")
fenetre.tk.call('wm', 'iconphoto', fenetre._w, tk.PhotoImage(file='test.png'))

def lignevide() :
    lignevide = Label(fenetre, text="" ,fg="red", font ="Arial" ,height ="1")
    lignevide.pack()

scrollbar = Scrollbar(fenetre)
scrollbar.pack(side='right', fill='y')

texte1 = Label(fenetre, text="Tapez votre texte à imprimer :" ,fg="black", font =("Montserrat" ,22) ,height ="1",)
texte1.pack()

texte2 = Label(fenetre, text= "Attention, les chiffres, les majuscules et certains caractères spéciaux ne sont pas imprimables !" ,fg="red", font =("Montserrat" ,10) ,height ="1",)
texte2.pack()

lignevide()

saisie = Text(fenetre, width = 25 ,height = 23, font="Arial" ,highlightcolor = "black" ,highlightbackground = "yellow", relief="flat", yscrollcommand = scrollbar.set)
saisie.pack()


lignevide()

bouton = ttk.Button(fenetre, text="Confirmer", command=retrieve_input)
bouton.pack()

lignevide()

bouton2 = ttk.Button(fenetre, text="Annuler et quitter", command=fin)
bouton2.pack()

lignevide()

fenetre.mainloop()
Thanks to anyone that would help me !
You have not included the two functions from the buttons' command= parameter, retrieve_input() and fin(). I would guess that limiting the lines of text would occur in retrieve_input function. Use a counter to count the number of lines already in the Text, and if it exceeds 25, retrieve_input would show a warning message instead of putting it in the Text. Note that you could also add a Scrollbar, which would allow for more than 25 lines of text, while the size of the Text widget would remain the same.
Yes, good solution but it forces the user to manually delete the lines that are over 23th. Do you know any way to prevent the user to write more than 23 lines instead of just warning him that there are too many lines ?

By the way thanks for your fast answer !
Quote:Do you know any way to prevent the user to write more than 23 lines instead of just warning him that there are too many lines ?

Quote:You have not included the two functions from the buttons' command= parameter, retrieve_input() and fin(). I would guess that limiting the lines of text would occur in retrieve_input function. Use a counter to count the number of lines already in the Text, and if it exceeds 25, retrieve_input would show a warning message instead of putting it in the Text