I wanted to monitor when the text in a tkinter Text
widget was modified so that a user could save any new data they had entered. Then on pressing 'Save' I wanted to reset this.
I bound the Text
widget's <<Modified>>
event to a function so that making any changes to the text would update the 'Save' button from 'disabled'
to 'normal'
state. After hitting the Save button I ran a function which reset the modified
flag and disabled the Save button again until further changes were made.
But I found that it seemed to only fire the event once. Hitting Save didn't reset the button to a 'disabled'
state, and editing the text didn't seem to affect the Save button's state either after the first time.
Below is a minimal example to show how the flag doesn't seem to be reset.
import tkinter as tk
root = tk.Tk()
def text_modified(event=None):
status_label.config(text="Modified = True")
def reset():
if not text_widget.edit_modified():
return
status_label.config(text="Modified = False")
text_widget.edit_modified(False)
text_widget = tk.Text(root, width=30, height=5)
text_widget.pack()
text_widget.bind("<<Modified>>", text_modified)
status_label = tk.Label(root, text="Modified = False")
status_label.pack()
reset_btn = tk.Button(root, text="Reset", command=reset)
reset_btn.pack()
root.mainloop()