Reputation: 309
When the user hovers the mouse over the pink rectangle I want the program to display a tooltip in the same way that it would if you were to hover your mouse over a button. However, this doesn't work because I get the error >AttributeError: 'Bar' object has no attribute 'tk'. How do I bind a tooltip to a canvas rectangle in tkinter?
from tkinter import *
import Pmw
root = Tk()
Pmw.initialise(root)
canvas = Canvas()
canvas.config(width=800, height=700, bg='white')
class Bar:
def __init__(self, x, y):
self.x = x
self.y = y
self.bar = canvas.create_rectangle(x, y, x + 200, y + 200, fill='pink')
bar = Bar(200, 200)
# create balloon object and bind it to the widget
balloon = Pmw.Balloon(bar)
balloon.bind(bar, "Text for the tool tip")
lbl = balloon.component("label")
lbl.config(background="black", foreground="white")
# Pmw.Color.changecolor(lbl, background="black", foreground="white")
canvas.pack()
root.mainloop()
Upvotes: 1
Views: 416
Reputation: 3089
Use balloon.tagbind(Canvas/Text, tag, "tooltip text")
.
Minimal example:
import Pmw
from tkinter import *
root = Tk()
Pmw.initialise(root)
canvas = Canvas(root, width=800, height=700, bg="white")
canvas.pack()
bar1 = canvas.create_rectangle(50, 50, 100, 100, fill="pink")
bar2 = canvas.create_rectangle(300, 300, 500, 500, fill="red")
balloon = Pmw.Balloon()
balloon.tagbind(canvas, bar1, "first tooltip")
balloon.tagbind(canvas, bar2, "second tooltip")
root.mainloop()
Upvotes: 2