Skip to main content
 首页 » 编程设计

python之PyGTK:移动自定义构建工具提示窗口

2025年05月04日9jackei

我已经构建了带有 matplotlib 图形的 PyGTK 应用程序。我还想使用自定义构建的工具提示窗口。工具提示值根据鼠标在图形上的位置而变化。

我的问题是,我无法将工具提示窗口移动到鼠标旁边,因为我不知道如何在屏幕中获取鼠标位置

这是我的代码:

def figPrepare(self):   #initialize graph 
        #figure preparation stuff 
 
        #custom tooltip window 
        tooltip = gtk.Window(gtk.WINDOW_POPUP) 
        lbl = gtk.Label() 
        tooltip.add(lbl) 
        lbl.show() 
 
        figure.canvas.set_tooltip_window(tooltip) 
        figure.canvas.props.has_tooltip = True 
        #events 
        figure.canvas.mpl_connect('figure_enter_event',lambda w: tooltip.show()) 
        figure.canvas.mpl_connect('motion_notify_event',lambda w: self.updateTooltip(tooltip, lbl)) 
        figure.canvas.mpl_connect('figure_leave_event',lambda w: tooltip.hide()) 
 
 
    def updateTooltip(self, win, lbl): 
        lbl.set_text(str(time.time())) 
        win.move(w.x, w.y) 

此代码移动工具提示窗口,但值基于 matplotlib 图形,而不是屏幕中的绝对位置。

谁能告诉我如何将工具提示窗口移动到鼠标指针旁边?

请您参考如下方法:

我找到了解决方案:

def updateTooltip(self, win, lbl): 
    lbl.set_text(str(time.time())) 
    x, y, mods = win.get_screen().get_root_window().get_pointer()   #this gets absolute mouse possition on screen 
    win.move(x+15, y+10)