如何在 tkinter canvas 中将 Pmw 工具提示添加到矩形对象?

How to add a Pmw tooltip to a rectangle object in a tkinter canvas?

当用户将鼠标悬停在粉红色矩形上时,我希望程序以与将鼠标悬停在按钮上时相同的方式显示工具提示。但是,这不起作用,因为我收到错误 >AttributeError: 'Bar' object has no attribute 'tk'。如何在 tkinter 中将工具提示绑定到 canvas 矩形?

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()

使用balloon.tagbind(Canvas/Text, tag, "tooltip text").

最小示例:

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()