首页 文章

删除tkinter文本默认绑定

提问于
浏览
4

我正在制作一个简单的tkinter文本编辑器,但我希望尽可能删除 Text widget 的所有默认绑定 .

例如,当我按 Ctrl + i 时,默认情况下会插入一个Tab字符 . 我做了一个事件绑定,打印文本框中有多少行,我也将事件绑定设置为 Ctrl + i .

当我运行它时,它会打印文本框内的行数,但也会插入制表符 .

我想知道我怎么能 Overwrite 默认绑定,或者学习如何删除所有默认绑定 .

继承了我的代码btw:

from tkinter import *

class comd: # Contains primary commands
    # Capital Rule ----------------------------
    # G = Get | I = Insert | D = Draw | S = Set
    # -----------------------------------------

    def Ggeo(self): # Get Geometry (Get window geometry)
        x = root.winfo_width()
        y = root.winfo_height()
        print("Current Window Geometry")
        print(str(x) + " x " +str(y))

    def Idum(self): # Insters "Dummy Insert"
        import tkinter as tkin
        tbox.insert(INSERT, "Dummy Insert")

    def Ilim(self): # Prints How many lines are in
        info =  int(tbox.index('end-1c').split('.')[0])
        print(info)



root = Tk()
root.geometry("885x600-25-25")

tbox = Text(root, font=("Courier","14","bold"))
tbox.pack(expand = True , fill = BOTH)


# Problem here --------------------
tbox.bind("<Control-i>", comd.Ilim)
# ---------------------------------


mainloop()

1 回答

  • 7

    您可以通过让函数返回字符串 "break" 来覆盖绑定 . 例如:

    def Ilim(self): # Prints How many lines are in
        info =  int(tbox.index('end-1c').split('.')[0])
        print(info)
        return "break"
    

    如果要完全删除所有绑定( including the bindings that allow you to insert characters ),可以轻松完成 . 所有绑定都与"bind tag"(或"bindtag")相关联 . 如果删除绑定标签,则删除绑定 .

    例如,这将删除所有默认绑定:

    bindtags = list(tbox.bindtags())
        bindtags.remove("Text")
        tbox.bindtags(tuple(bindtags))
    

    有关绑定标记的更多信息,请参阅以下答案:https://stackoverflow.com/a/11542200/7432

相关问题