如何使文本小部件只读

How do I make a Text widget readonly

我正在尝试将 TEXT 小部件设置为只读,以便用户可以查看但不能编辑它。我在另一个 SO 问题中看到了 "readonly" 的状态,但是它向我抛出了这个错误

_tkinter.TclError: bad state "readonly": must be disabled or normal

我的代码如下

e = Text(root ,height=10, width=50).config(state="readonly")
e.place(x=1,y=1)

Text 小部件的 state 没有这样的可能值 "readonly"。您可以禁用它,将状态设置为 "disabled"(您可以直接在构造函数中执行):

e = Text(root, height=10, width=50, state='disabled') # no need to call config

来自 Tk 文档:

If the text is disabled then characters may not be inserted or deleted and no insertion cursor will be displayed, even if the input focus is in the widget.

我认为你应该使用 Label,如果你只想显示一些文本,这就是标签存在的原因。