删除 tkinter 文本默认绑定 [英] Remove tkinter text default binding
问题描述
我正在制作一个简单的 tkinter 文本编辑器,但我希望尽可能删除 文本小部件 的所有默认绑定.
I'm making a simple tkinter Text editor, but i want all default bindings of the Text widget removed if possible.
例如,当我按 Ctrl + i 时,它默认插入一个制表符.我做了一个事件绑定来打印文本框中有多少行,我也将事件绑定设置为 Ctrl + i.
For example when i press Ctrl + i it inserts a Tab character by default. I made an event binding that prints how many lines are in the text box, I set the event binding to Ctrl + i as well.
当我运行它时,它会打印文本框内的行数,但还会插入一个制表符.
When i run it, It prints the number of lines inside the text box, but also inserts a tab character.
我想知道如何覆盖默认绑定,或了解如何删除所有默认绑定.
I want to know how i can Overwrite the default bindings, or learna way how to remove all the default bindings.
顺便说一句,这是我的代码:
Heres my code 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()
推荐答案
您可以通过让函数返回字符串 "break"
来覆盖绑定.例如:
You can overwrite a binding by having your function return the string "break"
. For example:
def Ilim(self): # Prints How many lines are in
info = int(tbox.index('end-1c').split('.')[0])
print(info)
return "break"
如果您想完全删除所有绑定(包括允许您插入字符的绑定),这很容易实现.所有绑定都与绑定标签"(或绑定标签")相关联.如果删除绑定标签,则删除绑定.
If you want to completely remove all bindings (including the bindings that allow you to insert characters), that can be easily accomplished. All of the bindings are associated with a "bind tag" (or "bindtag"). If you remove the bindtag, you remove the bindings.
例如,这将删除所有默认绑定:
For example, this removes all of the default bindings:
bindtags = list(tbox.bindtags())
bindtags.remove("Text")
tbox.bindtags(tuple(bindtags))
有关绑定标签的更多信息,请参阅此答案:https://stackoverflow.com/a/11542200/7432
For more information on bind tags, see this answer: https://stackoverflow.com/a/11542200/7432
这篇关于删除 tkinter 文本默认绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!