Python Forum
[Tkinter] Lock Text Widget height
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Lock Text Widget height
#1
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 !
Reply
#2
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.
Reply
#3
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 !
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,672 Jun-26-2022, 06:26 PM
Last Post: menator01
  Tkinter reduce OptionMenu height euras 2 4,569 May-25-2021, 09:14 PM
Last Post: euras
  [Tkinter] Text widget inert mode on and off rfresh737 5 3,784 Apr-19-2021, 02:18 PM
Last Post: joe_momma
  Line numbers in Text widget rfresh737 3 5,317 Apr-15-2021, 12:30 PM
Last Post: rfresh737
  tkinter text widget word wrap position chrisdb 6 7,459 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  Check for Caps Lock ebolisa 1 2,214 Dec-29-2020, 10:41 PM
Last Post: Larz60+
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,295 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  How to place global tk text widget in class or on canvas puje 1 2,283 Jul-04-2020, 09:25 AM
Last Post: deanhystad
  Treeview - How to lock columns taratata2020 0 3,291 Mar-29-2020, 03:30 PM
Last Post: taratata2020
  Tkinter - Need Help setting Height of a TextInputBox itslewis 1 1,522 Mar-22-2020, 02:37 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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