tkinter 中不可删除的文本

发布于 2024-12-08 23:45:31 字数 738 浏览 0 评论 0 原文

这是一些代码:

from Tkinter import *

class Main(object):

    def __init__(self):
        self.console = Text(root, relief='groove', cursor='arrow', spacing1=3)
        self.console.insert(INSERT, '>>> ')
        self.console.focus_set()
        self.scroll = Scrollbar(root, cursor='arrow', command=self.console.yview)
        self.console.configure(yscrollcommand=self.scroll.set)

        self.scroll.pack(fill='y', side='right')
        self.console.pack(expand=True, fill='both')

root = Tk()
root.geometry('%sx%s+%s+%s' %(660, 400, 40, 40))
root.option_add('*font', ('Courier', 9, 'bold'))
root.resizable(0, 1)
app = Main()
root.mainloop()

有没有办法使 '>>> ' 变得不可移除(例如在 IDLE 中)? 提前致谢。

here is some code:

from Tkinter import *

class Main(object):

    def __init__(self):
        self.console = Text(root, relief='groove', cursor='arrow', spacing1=3)
        self.console.insert(INSERT, '>>> ')
        self.console.focus_set()
        self.scroll = Scrollbar(root, cursor='arrow', command=self.console.yview)
        self.console.configure(yscrollcommand=self.scroll.set)

        self.scroll.pack(fill='y', side='right')
        self.console.pack(expand=True, fill='both')

root = Tk()
root.geometry('%sx%s+%s+%s' %(660, 400, 40, 40))
root.option_add('*font', ('Courier', 9, 'bold'))
root.resizable(0, 1)
app = Main()
root.mainloop()

is there some way to make '>>> ' become unremovable(like in IDLE for example)?
thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独﹏钓一江月 2024-12-15 23:45:31

看一下IDLE的源代码。特别是查看 EditorWindow.py 中的“smart_backspace_event”。 IDLE 将文本小部件上的 绑定到此函数(间接通过 <> 事件)。

您需要的基本代码如下所示:

chars = console.get("insert linestart", "insert")
# [Do some analysis on "chars" to detect >>> and prevent a backspace]

if DO_BACKSPACE: 
    console.delete("insert-1c", "insert")

# "break" is important so that the Text widget's backspace handler doesn't get called
return "break"

Take a look at IDLE's source code. In particular look at 'smart_backspace_event' in EditorWindow.py. IDLE binds <Key-Backspace> on the text widget to this function (indirectly through the <<smart-backspace>> event).

The basic code you'll need follows like this:

chars = console.get("insert linestart", "insert")
# [Do some analysis on "chars" to detect >>> and prevent a backspace]

if DO_BACKSPACE: 
    console.delete("insert-1c", "insert")

# "break" is important so that the Text widget's backspace handler doesn't get called
return "break"
没有你我更好 2024-12-15 23:45:31

没有内置的方法可以做到这一点。您必须设置一组覆盖默认行为的绑定,这并不是一件特别容易的事情。不过,这是可能的,因为您可以完全控制所有绑定(即:在无法更改的小部件中没有硬编码任何行为)

另一个更防弹的解决方案是拦截低级 tkinter 插入和删除命令,并检查某些条件。例如,请参阅问题的答案 https://stackoverflow.com/a/11180132/7432。该答案提供了一个通用解决方案,可用于提示(如本问题中所要求的),或将文本的任何部分标记为只读。

There is no built in way to do this. You will have to set up a collection of bindings that override the default behavior, and that's not a particularly easy thing to do. It's possible, though, since you have complete control over all bindings (ie: no behavior is hard-coded in the widget where it can't be changed)

Another solution which is more bullet proof is to intercept the low-level tkinter insert and delete commands, and check for some condition. For an example, see the answer to the question https://stackoverflow.com/a/11180132/7432. That answer provides a general solution that can be used for a prompt (as asked for in this question), or for tagging any sections of text as readonly.

心房的律动 2024-12-15 23:45:31

'>>>' IDLE 中显示的是 Python 解释器输出的一部分。我认为您可以尝试侦听 事件并在需要时恢复提示(请参阅 http://docs.python.org/library/tkinter.html#bindings-and-eventshttp://effbot.org/tkinterbook/tkinter-events-and-bindings.htm)

The '>>>' displayed in IDLE is a part of Python interpreter output. I think you can try listening for <Key> events and restoring the prompt when needed (see http://docs.python.org/library/tkinter.html#bindings-and-events and http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文