Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

tkinter
python

How to disable an entry widget in Tkinter

Gutha Vamsi Krishna

Overview

Tkinter is the standard GUI library for Python. We can create graphical applications using this library. In this Answer, we'll learn to disable an entry widget in Tkinter.

The entry widget is used to take input from the user. If we want to disable it, we need to pass disabled as a value to the state parameter in the config method.

Syntax

entry_widget.config(state="disabled")

Example

from tkinter import *

#get tinker instance frame
window = Tk()

#set window size
window.geometry("600x400")


#normal entry widget
entry=Entry(window, width= 30)
entry.pack(pady=20)

#disabled entry widget
entry_disabled=Entry(window, width= 30)
entry_disabled.pack(pady=20)
entry_disabled.config(state= "disabled")


window.mainloop()

Explanation

  • Line 1: We import everything from tkinter package.
  • Line 4: We create an instance of the Tkinter frame and store it in the window variable. We do so to keep widgets inside of the Tkinter frame in order to display them.
  • Line 7: We set the window size of the Tkinter frame.
  • Line 11–12: We create the normal entry widget, with which users can interact.
  • Line 15–17: We create the entry widget and disable it using the config method and pass disabled as a value to the state parameter.

After disabling, users won't be able to interact with the second entry widget.

RELATED TAGS

tkinter
python

CONTRIBUTOR

Gutha Vamsi Krishna
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring