这是一些代码:
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中)?提前致谢。
看一下IDLE的源代码。尤其要注意EditorWindow.py中的“ smart_backspace_event”。IDLE<Key- Backspace>在文本窗口小部件上绑定到此功能(通过<<smart-backspace>>事件间接)。
<Key- Backspace>
<<smart-backspace>>
您将需要的基本代码如下所示:
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"