Skip to Content
Tkinter GUI Programming by Example
book

Tkinter GUI Programming by Example

by David Love
April 2018
Beginner content levelBeginner
340 pages
7h 54m
English
Packt Publishing
Content preview from Tkinter GUI Programming by Example

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Tkinter GUI Application Development Blueprints, Second Edition - Second Edition

Tkinter GUI Application Development Blueprints, Second Edition - Second Edition

Bhaskar Chaudhary

Publisher Resources

ISBN: 9781788627481Supplemental Content