O'Reilly logo

Tkinter GUI Programming by Example by David Love

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Overwriting default events

What happens if we bind a key which already has default behavior on the target widget? Let's take a look at one example. You should remember from our text editor example that the combination of Ctrl + O inserted blank lines. We should overwrite that:

import tkinter as tkwin = tk.Tk()text = tk.Text(win, fg="black", bg="white")text.bind('<Control-o>', lambda e, t=text: t.insert(1.0, 'aaa'))text.pack()win.mainloop()

Run this example, type three lines of dummy text, then place your cursor in the middle somewhere. When you press Ctrl + O, you would expect the letters aaa to be inserted at the beginning of the editor. Well, they are, but the default behavior of adding blank lines also happens. If we are going to bind ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required