How to bind events to Canvas items?

To interact with objects contained in a Canvas object you need to use tag_bind() which has this format: tag_bind(item, event=None, callback=None, add=None) The item parameter can be either a tag or an id. Here is an example to illustrate the concept: from tkinter import * def onObjectClick(event): print(‘Got object click’, event.x, event.y) print(event.widget.find_closest(event.x, event.y)) root … Read more

Disable the underlying window when a popup is created in Python TKinter

If you don’t want to hide the root but just make sure the user can only interact with the popup, you can use grab_set() and grab_release(). Example app, tested with Python 3.7 and 3.8: import tkinter as tk import sys import platform class Popup: def __init__(self): self.tl = None self.root = tk.Tk() self.root.title(“Grab Set/Release”) tk.Label(self.root, … Read more

Display message when hovering over something with mouse cursor in Python

I think this would meet your requirements. Here’s what the output looks like: First, A class named ToolTip which has methods showtip and hidetip is defined as follows: from tkinter import * class ToolTip(object): def __init__(self, widget): self.widget = widget self.tipwindow = None self.id = None self.x = self.y = 0 def showtip(self, text): “Display … Read more

Tkinter KeyPress and KeyRelease events

Ok some more research found this helpful post which shows this is occuring because of X’s autorepeat behaviour. You can disable this by using os.system(‘xset r off’) and then reset it using “on” at the end of your script. The problem is this is global behaviour – not just my script – which isn’t great … Read more

How to add space between two widgets placed in grid in tkinter ~ python?

When you pack the widget you can use self.a_button = Button(root, text=”A Button”) self.a_button.grid(row=0, column=1, padx=10, pady=10) Using padx and pady you can add padding to the outer side of the button and alternatively if you want to increase the size of the button you can add inner padding using ipadx and ipady. If you … Read more