【发布时间】:2014-03-19 08:28:22
【问题描述】:
我想将tkinter text widget 用作readonly 小部件。它应该充当transcript 区域。我的想法是将此记录保存在file 中,每当用户编写任何内容时,只需删除小部件的所有内容,然后重新编写即可。
代码如下:
transcript_entry = SimpleEditor() # SimpleEditor is inherited from ScrolledText
transcript_entry.text.delete("1.0", END)
# this is just a test string, it should be the contents of the transcript file
transcript_entry.text.insert("1.0", "This is test transcript")
transcript_entry.text.bind("<KeyPress>", transcript_entry.readonly)
readonly 函数看起来像:
def readonly(self, event):
self.text.delete("1.0", END)
# this is just a test string, it should be the contents of the transcript file
self.text.insert("1.0", "This is test transcript")
这里的错误是用户输入的最后一个字符被添加到脚本中。我怀疑原因是调用了只读函数,then 用户输入被写入小部件。如何颠倒这个顺序并让 readonly 函数被调用 after 用户输入被写入小部件?
有什么提示吗?
【问题讨论】:
-
什么是 ScrolledText 小部件?我不熟悉它...
-
设置
state=DISABLED怎么样? -
类似于带有滚动条的
tkinter text widgetdocs.python.org/2/library/scrolledtext.html -
如果 state=DISABLED,您将无法选择和复制部分文本
-
您在插入字符之前重置文本。请改用
<KeyRelease>。但我想仍然必须有更好的方法......也许this 有帮助?