Bottom Page

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 !
Quote
#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.
Quote
#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 !
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Tkinter - Need Help setting Height of a TextInputBox itslewis 1 83 Mar-22-2020, 02:37 AM
Last Post: deanhystad
  [Tkinter] manipulation of string in Text widget Stauricus 2 145 Feb-17-2020, 09:23 PM
Last Post: Stauricus
  [Tkinter] Paste Operation not working in Text Widget Code_Enthusiast 1 401 Sep-11-2019, 08:49 PM
Last Post: Larz60+
  how to insert image into Text widget Tkinter atlass218 5 2,880 Apr-17-2019, 05:28 AM
Last Post: atlass218
  [Tkinter] update the content of Text widget atlass218 10 6,642 Dec-15-2018, 11:51 AM
Last Post: atlass218
  [Tkinter] Two text files in Text widget (python 3) atlass218 21 3,778 Nov-09-2018, 06:24 PM
Last Post: atlass218
  Canvas Text Width/Height Anysja 0 1,302 Aug-20-2018, 11:11 PM
Last Post: Anysja
  How to delete text from a tkinter Text widget? Tang 1 18,707 May-20-2018, 09:26 PM
Last Post: Larz60+
  [Tkinter] Class with text widget method AeranicusCascadia 3 4,021 Nov-14-2017, 11:33 PM
Last Post: AeranicusCascadia
  [Tkinter] Scroll bar height is not fixed with Text widget MeeranRizvi 2 6,592 Feb-06-2017, 12:24 PM
Last Post: MeeranRizvi

Forum Jump:


Users browsing this thread: 1 Guest(s)