Search code examples
pythonpython-3.xtkinterkey-bindings

How to unbind Ctrl-i in python which is by default bind to "Tab" key


i made a text editor in python. by default Ctrl+i is bind to Tab key. whenever i press Ctrl+i it makes the font italics but also move the cursor to one 'tab'. how can i unbind the Ctrl+i with Tab key.

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()

Solution

  • You don't need to remove the default binding. The way bindings work in tkinter is that first your custom binding is applied and then the default binding. To prevent the default binding from happening, your function simply needs to return the string "break".