HomeLinuxTkinter Textual content Field

Tkinter Textual content Field


Tkinter is a built-in library of Python that permits the person to construct Graphical Person Interfaces. When working with Graphical Person Interfaces, the person has to formulate a number of parts, together with a textual content field. For this, Tkinter has a built-in textual content widget.

The Tkinter Textual content Field, because the identify says, incorporates textual content containers contained in the GUI created via tkinter. This submit will reveal the working and examples of Tkinter Textual content Containers.

Tkinter Textual content Field | Tkinter Textual content Widget

To create a textual content field utilizing the Tkinter, the person has to make use of the “Textual content()” perform. This “Textual content” perform will create a textual content field that the person has so as to add to the “Body” of Tkinter.  This Textual content() is usually known as the “Textual content Widget”. To know the working of the textual content widget, check out the syntax under:

Syntax of Tkinter Textual content Widget

varX = Textual content (parentFrame, optionsParams)

 
Within the given syntax:

    • varX is the variable that will likely be used to consult with and retailer the state of the tkinter textual content widget.
    • parentFrame is the window/body that may maintain the textual content field inside it.
    • optionsParams are the completely different choices that can be utilized to customise the tkinter textual content field

Word: To study extra in regards to the customization choices, learn this official documentation from Tkinter!

Learn how to Create a Tkinter Textual content Field?

To create a primary textual content field utilizing tkinter, begin by initializing importing tkinter in your program and initializing a window utilizing the next traces of code:

from tkinter import *
tkWindow = Tk();
tkWindow.geometry(“500×500”)

 
After that, initialize the textual content field widget with a particular top and width:

textBox = Textual content(tkWindow,top=15,width=15);

 
After that, set the “pack” property of the textual content widget to increase:

textBox.pack(increase=True)

 
Word: Doing this can permit the textual content field to take up the required area on the tkinter window.

Lastly, set the tkinter window to a loop in order that it doesn’t shut itself after just one body:

 
The whole snippet is as:

from tkinter import *
# Tkinter Window
tkWindow = Tk();
tkWindow.geometry(“500×500”)
# Tkinter Textual content Field
textBox = Textual content(tkWindow,top=15,width=60);
textBox.pack(increase=True)
tkWindow.mainloop()

 
Operating this code snippet will produce the next Tkinter window:


This textual content field window is editable, which implies that the person can simply add textual content inside it:


A easy textual content field was simply created inside Tkinter.

Learn how to Create a Non-Editable Textual content Field Utilizing Tkinter?

To create a non-editable textual content field contained in the Tkinter GUI, the person has to create a textual content widget after which set its config state to “disabled”. To take an instance of this, use the next code:

from tkinter import *
# Tkinter Window
tkWindow = Tk();
tkWindow.geometry(“500×500”)
textual content = ‘Good day, That is LinuxHint.
        It is a non-editable Textual content Field
        Made with Tkinter.    
        ‘

# Tkinter textual content field
textBox = Textual content(tkWindow,top=15,width=60);
textBox.insert(“finish”,textual content)
# To make it non-editable use the config perform
textBox.config(state=‘disabled’)
textBox.pack(increase=True)
tkWindow.mainloop()

 
On this code snippet:

    • A easy window has been created with a textual content field
    • A string is inserted into the textual content field utilizing the “insert()” perform.
    • The config() perform is used to set the “state” of the textbox to “disabled” which makes it non-changeable.

Operating the above code produces the next Tkinter window:

Learn how to Delete the Content material of the Tkinter Textual content Field?

To delete the entire content material of the Tkinter Textual content field, merely use the “delete()” perform with the textBox variable. To reveal this, take the next code:

from tkinter import *
# Tkinter Window
tkWindow = Tk();
tkWindow.geometry(“500×500”)
textual content = ‘Good day, That is LinuxHint.
        Press the Button under to filter the content material of the Textual content Field    
        ‘

# Tkinter textual content field
textBox = Textual content(tkWindow,top=15,width=60);
textBox.insert(“finish”,textual content)
textBox.pack(increase=True)

def clear_text():
    textBox.delete(1.0, END)

Button(tkWindow, textual content=‘Clear’, width=15, top=2,
       command=clear_text).pack(increase=True)
tkWindow.mainloop()

 
On this above code snippet:

    • A easy textual content field has been added with some content material inside it within the Tkinter Window.
    • A button has additionally been added that calls the “clear_text()” perform.
    • The “clear_text()” perform takes the textual content field variables and calls the “delete()” perform, passing the arguments as “1.0” and “END”, which is used to clear the whole content material.

Operating the above code snippet will produce the next Tkinter Window:


The output verified that the button deleted the content material of the Tkinter Textual content Widget.

Conclusion

A Tkinter Textual content Field, or the Tkinter Textual content Widget, creates textual content containers on the home windows created via Tkinter. These textual content containers might be made to carry a pre-defined textual content and user-added textual content at run time. Furthermore, the person can change the working and the looks of the textual content field by utilizing completely different flags and features on the textual content field. This submit has illustrated the creation of a textual content field utilizing the Tkinter textual content field with examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments