AqwamCreates
AqwamCreates

Reputation: 53

How to insert text to scrolledtext while it is in disabled state in tkinter?

Full Explanation: I wanted to add a text to scrolledtext when user replies in the entry box AND I do not want them to change the text inside scrolledtext.

In other words: a chat log.


myWindow = tk.Tk()
frame_Chat = tk.Frame(myWindow)

#This will be scrolledText
CH = scrolledtext.ScrolledText(frame_Chat)
CH.grid(row = 1, column =0)

#This will be reply box 
reply = tk.Entry(frame_Chat, bg = "white", width = 70)
reply.grid(row = 6 , column = 0)

def send():
    r = reply.get()
    CH.insert(tk.INSERT, "name: " + r + "\n")

ReplyButton = tk.Button(frame_Chat, text = "Send", command = send)
ReplyButton.grid(row = 6, column = 1)

myWindow.mainloop()

I wanted to make the coding as simple as possible by adding a transparent canvas over the scrolledtext, so that the user cannot edit anything inside the scrolledtext, while allowing it in "enabled" state.

I tried to find this way, but since I'm new to Tkinter... it's probably impossible for me to find the answer...

if the first method is not possible, then suggest another method. Thanks for reading!

Upvotes: 2

Views: 2270

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385870

You need to temporarily set the state to "normal", insert the text, then set the state back to "disabled".

CH.configure(state="normal")
CH.insert(tk.INSERT, "name: " + r + "\n")
CH.configure(state="disabled")

Upvotes: 2

AqwamCreates
AqwamCreates

Reputation: 53

These people don't understand the part where I said I'm a beginner to tkinter and only give hints for the code. I had to search up what they meant and it took me some time to find it.

Just insert CH.config(state = "normal") to switch between disabled and normal state. The lines of code required are seen here


def send():
    r = reply.get()
    CH.config(state = "enabled")
    CH.insert(tk.INSERT, "name: " + r + "\n")
    CH.config(state = "disabled)

sigh

Upvotes: 1

Related Questions