Tkinter - Read only Entry Widget
Last Updated :
09 Dec, 2021
Improve
Python has a number of frameworks to develop GUI applications like PyQT, Kivy, Jython, WxPython, PyGUI, and Tkinter. Python tkinter module offers a variety of options to develop GUI based applications. Tkinter is an open-source and available under Python license. Tkinter provides the simplest and fastest way to develop a GUI application. Tkinter supports different widgets of which the Entry widget is used to accept input from the user. However, there are cases when a certain text needs to be in a read-only format to prevent alterations by the user. This can be achieved by the Entry widget and the various options available under the Entry widget.
In this article, we shall see the use of Tkinter variables. Python variables can be used Tkinter widgets but do not provide the flexibility as provided by Tkinter variables. Tkinter variables have a unique feature called 'tracing' that can be used to trace changes made to associated variables. This is useful to track accidental changes made to a variable while working. The code below demonstrates the creation of a read-only Entry widget using Tkinter.
Python3 1==
Output
-
Import tkinter module
import tkinter
-
Import tkinter sub-module
from tkinter import *
-
Creating the parent widget
root = Tk()
Syntax: Tk(screenName=None, baseName=None, className='Tk', useTk=1) Parameter: In this example, Tk class is instantiated without arguments. Explanation: This method creates a blank parent window with close, maximize, and minimize buttons on the top. -
Creating Labels for the entry widgets and positioning the labels in the parent widget
L1 = Label(root, text="User Name") L1.grid(row=0, column=0) L2 = Label(root, text="Password") L2.grid(row=1, column=0)
Syntax: Label(master, **options) Parameter:- master: The parent window (root) acts as the master.
- options: The options supported by Label() method are text, anchor, bg, bitmap, bd, cursor, font, fg, height, width, image, justify, relief, padx, pady, textvariable, underline and wraplength. Here the text option is used to display the name of the entry widget.
- options: The options available under the grid() method which can be used to alter the position of a widget in the parent widget are row, rowspan, column, columnspan, padx, pady, ipadx, ipady and sticky.
-
Creating a Tkinter variable for the Entry widget
mystr = StringVar()
Syntax: StringVar() Parameter: The constructor takes no argument. To set the value, set() method is used. Explanation: StringVar is one of the inbuilt variable classes in Tkinter. The default value of StringVar() is an empty string "" . -
Setting the string value
mystr.set('username@xyz.com')
Syntax: set(string) Parameter:- string: Represents the text to be associated with the widget (here 'entry' widget)
-
Creating Entry widget
entry = Entry(textvariable=mystr, state=DISABLED).grid(row=0, column=1, padx=10, pady=10) mystr.set('username@xyz.com') passwd = Entry().grid(row=1, column=1, padx=10, pady=10)
Syntax: Entry(master, **options) Parameter:- master: Represents the parent widget (here root) .
- options: The options available under Entry widget are bg, bd, command, cursor, font, exportselection, justify, relief, highlightcolor, fg, selectbackground, selectforeground, selectborderwidth, show, xscrollcommand, state, textvariable and width.
-
Run the application
mainloop()
Syntax: mainloop() Explanation: The mainloop() basically acts like an infinite loop. It is used to run an application.
import tkinter
from tkinter import *
root = Tk()
L1 = Label(root, text="User Name")
L1.grid(row=0,column=0)
L2 = Label(root, text="Password")
L2.grid(row=1,column=0)
mystr = StringVar()
mystr.set('username@xyz.com')
entry = Entry(textvariable=mystr,
state=DISABLED).grid(row=0,
column=1,
padx=10,
pady=10)
passwd = Entry().grid(row=1,column=1,
padx=10,pady=10)
mainloop()
