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

How do I remove the light grey border around my Canvas widget?

Section 6.8 Why doesn’t the canvas seem to start at 0,0? of the Tk Usage FAQ describes the phenomenon. I was able to eliminate the border artefact with slight changes to the posted source… Change this: w = Canvas(master, width=150, height=40, bd=0, relief=”ridge”) w.pack() to: w = Canvas(master, width=150, height=40, bd=0, highlightthickness=0, relief=”ridge”) w.pack() and … Read more

Tkinter: How to get frame in canvas window to expand to the size of the canvas?

Just for future reference in case anyone else needs to know: frame = Frame(self.bottom_frame) frame.pack(side = LEFT, fill = BOTH, expand = True, padx = 10, pady = 10) self.canvas = Canvas(frame, bg = ‘pink’) self.canvas.pack(side = RIGHT, fill = BOTH, expand = True) self.mailbox_frame = Frame(self.canvas, bg = ‘purple’) self.canvas_frame = self.canvas.create_window((0,0), window=self.mailbox_frame, anchor … Read more

tkinter: using scrollbars on a canvas

Your scrollbars need to have the Frame as a parent, not the Canvas: from tkinter import * root=Tk() frame=Frame(root,width=300,height=300) frame.pack(expand=True, fill=BOTH) #.grid(row=0,column=0) canvas=Canvas(frame,bg=’#FFFFFF’,width=300,height=300,scrollregion=(0,0,500,500)) hbar=Scrollbar(frame,orient=HORIZONTAL) hbar.pack(side=BOTTOM,fill=X) hbar.config(command=canvas.xview) vbar=Scrollbar(frame,orient=VERTICAL) vbar.pack(side=RIGHT,fill=Y) vbar.config(command=canvas.yview) canvas.config(width=300,height=300) canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set) canvas.pack(side=LEFT,expand=True,fill=BOTH) root.mainloop() The reason why this works is due to how pack works. By default it will attempt to shrink (or grow) a … Read more