How to disable an Entry widget in Tkinter?

TkinterPythonGUI-Programming

Tkinter Entry widget accepts single line user-input in an entry field. You can customize the width, background color and size of the entry widget based on the need of your application.

Let us assume that in a particular application, we want to disable an Entry widget. To disable the Entry widget, use state='disabled' property in the constructor. Disabling the Entry widget will not allow users to edit and add values to it.

Example

Let us understand this with an example. In this example, we will create an Entry widget using the constructor Entry(master, **options) and a Button to disable it. The function disable_entry() will disable (grey out) the Entry widget.

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame
win = Tk()

# Set the size of the tkinter window
win.geometry("700x350")

def disable_entry():
   entry.config(state= "disabled")

# Create an entry widget
entry=Entry(win, width= 40, font= ('Helvetica 16'))
entry.pack(pady=20)

# Create a button
button=Button(win, text="Disable Entry", font=('Arial', 12), command=disable_entry)
button.pack()

win.mainloop()

Output

If you run the above code, it will display a window with an Entry widget and a button to disable this Entry widget.

Now, click the button "Disable Entry" to disable the Entry widget.

raja
Published on 22-Dec-2021 11:10:31
Advertisements