Skip to content Skip to sidebar Skip to footer

Creating A Mouse Over Text Box In Tkinter

I'm trying to implement system where when the user points to an object, a text box appears with certain information which I haven't implemented yet, then disappears when they move

Solution 1:

A bounding box (bbox) in tkinter is a 4-tuple which stores the bounds of the rectangle. You are only passing in the mouse location, which is a 2-tuple.

Also, you are never actually assigning to the variable "currentmouseoverevent" before using it in the code you show, so your closemouseover function will fail.

Solution 2:

The corrected code is as follows.

It turns out I was calling bbox wrong. Instead of passing the coords as a tuple, I should have passed them as the first four agrguments of create_rectangle. c.destroy is only for objects like canvas, entry or textbox, instead I used c.delete for deleting items, and used the event number returned by c.create_rectangle and c.create_text.

from tkinter import *

xhig, yhig = 425,325
bkgnclr = '#070707'
currentmouseoverevent = ['','']

c = Canvas(master, width=xhig*2, height=yhig*2, bg=bkgnclr, cursor = 'crosshair',)

def mouseovertext(event):
    mouseover = "Jack"if currentmouseoverevent[0] != '':
    closemouseover()
    currentmouseoverevent[0]=''return
currentmouseoverevent[0] = c.create_rectangle(event.x,event.y, (event.x + 5), (event.y +len(mouseover)*5),outline="white", fill=bkgnclr, width= len(mouseover))
 currentmouseoverevent[1] = c.create_text(event.x,event.y,text=mouseover, fill="white", currentmouseoverevent=event,anchor=NW)

def closemouseover(x):
    c.delete(currentmouseoverevent[0])
    c.delete(currentmouseoverevent[1])

c.bind("<Button-3", mouseovertext)

Post a Comment for "Creating A Mouse Over Text Box In Tkinter"