tkinter 中不可删除的文本 [英] unremovable text in tkinter

查看:62
本文介绍了tkinter 中不可删除的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一些代码:

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 中)?提前致谢.

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

推荐答案

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

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"

这篇关于tkinter 中不可删除的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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