禁止用户删除 tkinter scrolledtext 中的文本 [英] Disabling user from deleting text in tkinter scrolledtext
问题描述
如果已经发布并回答了我无法通过搜索找到它.我正在尝试使用 tkinter scrolledtext 小部件作为我正在编写的脚本将执行的文件操作的基本文本显示.我为什么要这样做?我不喜欢终端如何限制您可以滚动的距离和外观,而且我认为这看起来更加简化为简单的 GUI.
If this has already been posted and answered I couldn't find it by searching. I am trying to use a tkinter scrolledtext widget to serve as a basic text display for file operations the script I'm writing will perform. Why would I want to do this? I don't like how the terminal limits how far you can scroll, how it looks, and I think this looks much more streamlined into a simple GUI.
我知道我们可以像这样创建一个简单的滚动文本小部件,并通过将状态设置为 state = disabled
来禁用用户输入和删除.由于这些只是作为一个文本区域来显示正在发生的事情,我不需要来自用户的任何输入,只是一个只读界面.但是,将状态设置为禁用可防止 insert
用于插入更新本身.
I know we can create a simple scrolledtext widget like so and also disable user input and deleting with setting the state to state = disabled
. Since these is just serving as a text area to display essentially what's happening I don't need any input from the user merely a read-only interface. However, setting the state to disabled prevents insert
from being used to insert the updates themselves.
有没有办法解决这个问题?也欢迎使用更简单的替代方法.我可以使用标签,但随后会出现滚动问题.
Is there a way around this? Simpler alternative methods are also welcome. I could use a label, but then I'd have scrolling issues.
我意识到我可以改变插入前后的状态,从正常到禁用,但似乎应该有一个更简单的解决方案.
I realize I could change the state before and after inserting, from normal to disabled, but seems like there should be a simpler solution.
text_area = ScrolledText(master = root, wrap = tk.WORD, state = tk.DISABLED)
text_area.pack(side = tk.TOP, fill = tk.BOTH, expand = True)
text_area.insert(tk.INSERT, some_text_here) #No longer works
推荐答案
将状态从禁用设置为正常,插入文本,然后将其设置回禁用,是最简单的解决方案.
Setting the state from disable to normal, inserting text, and setting it back to disabled, is the simplest solution.
这篇关于禁止用户删除 tkinter scrolledtext 中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!