[英]Python tkinter text widget 'Modified' event doesn't seem to fire correctly
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.我想监视 tkinter
Text
小部件中的文本何时被修改,以便用户可以保存他们输入的任何新数据。 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.我将
Text
小部件的<<Modified>>
事件绑定到 function,以便对文本进行任何更改都会将“保存”按钮从'disabled'
更新为'normal'
state。点击保存按钮后,我运行了一个 function,它重置了modified
的标志并再次禁用“保存”按钮,直到进行进一步的更改。
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.点击保存并没有将按钮重置为
'disabled'
state,并且在第一次之后编辑文本似乎也没有影响保存按钮的 state。
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()
It turns out that binding the <<Modified>>
event to a function means that the function will run not when the Text
widget text is changed, but whenever the modified
flag is changed - whether it changes to True
or to False
.事实证明,将
<<Modified>>
事件绑定到 function 意味着 function 不会在Text
小部件文本更改时运行,而是在modified
标志更改时运行 - 无论它更改为True
还是False
。 So my Save button was saving the data, disabling itself, and resetting the modified
flag to False
, and this flag change fired the <<Modified>>
event, which was bound to a function which un-disabled the Save button again.所以我的 Save 按钮正在保存数据,禁用自身,并将
modified
的标志重置为False
,并且此标志更改触发了<<Modified>>
事件,该事件绑定到 function 再次取消禁用 Save 按钮。
Here's a minimal example which shows what's going on.这是一个显示正在发生的事情的最小示例。 We just need to adjust the function we've bound the
<<Modified>>
event to so that it deals with modified
being False
as well:我们只需要调整我们绑定
<<Modified>>
事件的 function,以便它也处理modified
为False
的情况:
import tkinter as tk
root = tk.Tk()
def modified_flag_changed(event=None):
if text_widget.edit_modified():
status_label.config(text="Modified = True")
print("Text modified")
else:
print("Modified flag changed to False")
def reset():
if not text_widget.edit_modified():
print("Doesn't need resetting")
return
status_label.config(text="Modified = False")
text_widget.edit_modified(False)
print('Reset')
text_widget = tk.Text(root, width=30, height=5)
text_widget.pack()
text_widget.bind("<<Modified>>", modified_flag_changed)
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.