如何禁用文本小部件的输入但允许编程输入? [英] How to disable input to a Text widget but allow programatic input?

查看:45
本文介绍了如何禁用文本小部件的输入但允许编程输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何锁定 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

  1. 将小部件的状态更改为NORMAL
  2. 插入文本,然后
  3. 将状态改回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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆