如何禁用文本小部件的输入但允许编程输入? [英] How to disable input to a Text widget but allow programatic input?
问题描述
我将如何锁定 Text
小部件,以便用户只能从中选择和复制文本,但我仍然可以将文本插入 Text代码>来自函数或类似的?
How would i go about locking a Text
widget so that the user can only select and copy text out of it, but i would still be able to insert text into the Text
from a function or similar?
推荐答案
您是否尝试过简单地禁用文本小部件?
Have you tried simply disabling the text widget?
text_widget.configure(state="disabled")
在某些平台上,您还需要在 <1>
上添加绑定以将焦点赋予小部件,否则不会出现复制突出显示:
On some platforms, you also need to add a binding on <1>
to give the focus to the widget, otherwise the highlighting for copy doesn't appear:
text_widget.bind("<1>", lambda event: text_widget.focus_set())
如果您禁用小部件,要以编程方式插入,您只需要
If you disable the widget, to insert programatically you simply need to
- 将小部件的状态更改为
NORMAL
- 插入文本,然后
- 将状态改回
DISABLED
只要您不在中间调用 update
,那么用户就无法以交互方式输入任何内容.
As long as you don't call update
in the middle of that then there's no way for the user to be able to enter anything interactively.
这篇关于如何禁用文本小部件的输入但允许编程输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!