How to make the Tkinter text widget read only?

PythonTkinterGUI-Programming

In Tkinter, sometimes, we may want to make a text widget disabled. To achieve this, we can set the text configuration as DISABLED. This will freeze the text widget and will make it read-only.

In this example, we will create a text widget and a button which will allow users to disable or freeze the text widget instantly.

Example

#Import the library
from tkinter import *

#Create an instance of window
win= Tk()

#Set the geometry of the window
win.geometry("700x400")

def disable_button():
   text.config(state= DISABLED)

#Label
Label(win,text="Type Something",font=('Helvetica bold', 25),
fg="green").pack(pady=20)

#Create a Text widget
text= Text(win, height= 10,width= 40)
text.pack()

#Create a Disable Button
Button(win, text= "Disable", command= disable_button,fg= "white",
bg="black", width= 20).pack(pady=20)

win.mainloop()

Output

Running the above code will create a text widget and a button which can be used to disable or freeze the widget.

After you click the “Disable” button, the text widget will be disabled and you won’t be able to type anything else inside it.

raja
Published on 06-Mar-2021 12:52:27
Advertisements