如何在默认情况下绑定到 "Tab" 键的 python 中取消绑定 Ctrl-i
How to unbind Ctrl-i in python which is by default bind to "Tab" key
我在 python 中制作了一个文本编辑器。默认情况下 Ctrl+i 绑定到 Tab 键。每当我按 Ctrl+i 时,它都会使字体变为斜体,但也会将光标移动到一个 'tab'。怎么解除Ctrl+i和Tab键的绑定
import tkinter as tk
from tkinter import ttk
from tkinter import font, colorchooser, filedialog, messagebox
import os
main_application = tk.Tk()
main_application.geometry('1200x800')
main_application.title('Rpad')
text_editor = tk.Text(main_application)
text_editor.config(wrap='word', relief=tk.FLAT)
tool_bar = ttk.Label(main_application)
tool_bar.pack(side=tk.TOP, fill=tk.X)
# italic button
italic_icon = tk.PhotoImage(file='icons2/italic.png')
italic_btn = ttk.Button(tool_bar, image=italic_icon)
italic_btn.grid(row=0, column=3, padx=5)
# italic functionlaity
def change_italic(event=None):
text_property = tk.font.Font(font=text_editor['font'])
if text_property.actual()['slant'] == 'roman':
text_editor.configure(
font=(current_font_family, current_font_size, 'italic'))
if text_property.actual()['slant'] == 'italic':
text_editor.configure(
font=(current_font_family, current_font_size, 'normal'))
italic_btn.configure(command=change_italic)
main_application.bind("<Control-i>", change_italic)
text_editor.focus_set()
text_editor.pack(fill=tk.BOTH, expand=True)
## font family & font size functionality ##
default_font_family = 'Arial'
default_font_size = 12
current_font_family = 'Arial'
current_font_size = 12
main_application.mainloop()
您不需要删除默认绑定。绑定在 tkinter 中的工作方式是首先应用您的自定义绑定,然后应用默认绑定。为了防止发生默认绑定,您的函数只需要 return 字符串 "break".
我在 python 中制作了一个文本编辑器。默认情况下 Ctrl+i 绑定到 Tab 键。每当我按 Ctrl+i 时,它都会使字体变为斜体,但也会将光标移动到一个 'tab'。怎么解除Ctrl+i和Tab键的绑定
import tkinter as tk
from tkinter import ttk
from tkinter import font, colorchooser, filedialog, messagebox
import os
main_application = tk.Tk()
main_application.geometry('1200x800')
main_application.title('Rpad')
text_editor = tk.Text(main_application)
text_editor.config(wrap='word', relief=tk.FLAT)
tool_bar = ttk.Label(main_application)
tool_bar.pack(side=tk.TOP, fill=tk.X)
# italic button
italic_icon = tk.PhotoImage(file='icons2/italic.png')
italic_btn = ttk.Button(tool_bar, image=italic_icon)
italic_btn.grid(row=0, column=3, padx=5)
# italic functionlaity
def change_italic(event=None):
text_property = tk.font.Font(font=text_editor['font'])
if text_property.actual()['slant'] == 'roman':
text_editor.configure(
font=(current_font_family, current_font_size, 'italic'))
if text_property.actual()['slant'] == 'italic':
text_editor.configure(
font=(current_font_family, current_font_size, 'normal'))
italic_btn.configure(command=change_italic)
main_application.bind("<Control-i>", change_italic)
text_editor.focus_set()
text_editor.pack(fill=tk.BOTH, expand=True)
## font family & font size functionality ##
default_font_family = 'Arial'
default_font_size = 12
current_font_family = 'Arial'
current_font_size = 12
main_application.mainloop()
您不需要删除默认绑定。绑定在 tkinter 中的工作方式是首先应用您的自定义绑定,然后应用默认绑定。为了防止发生默认绑定,您的函数只需要 return 字符串 "break".