gpt4 book ai didi

python - tkinter 中不可移动的文本

转载 作者:太空狗 更新时间:2023-10-30 02:34:33 24 4
gpt4 key购买 nike

这里是一些代码:

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 绑定(bind) <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"

关于python - tkinter 中不可移动的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7725009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com