trouble creating tooltips using Wx in Tk canvas

Ravikanth vvnrk.vanapalli at gmail.com
Wed Jul 6 11:12:04 EDT 2011


Hi all,
Hi all,

I am having a trouble creating tooltips. I am trying to embed a
matplotlib graph inside  a TkInter canvas. After some search over the
net, I found out a way to create tooltips using the WXbackend.
But when I embed my matplotlib figure inside a Tk and  the tooltips
are not getting displayed.
The error I am getting is below.

Traceback (most recent call last):
  File "U:\graphing_tool\plotinsideTkInter\plotInTk.py", line 12, in
<module>
    tooltip = wx.ToolTip(tip='tip with a long %s line and a newline\n'
% (' '*100)) # needs to be added to getover the bug with tooltip.
  File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_misc.py",
line 771, in __init__
    _misc_.ToolTip_swiginit(self,_misc_.new_ToolTip(*args, **kwargs))
PyNoAppError: The wx.App object must be created first!

I am not able to figure out the reason. I am trying to use IDLE for
running the script using python 2.7. I tried running from commandline
as well but still i face the same error. Following is the piece of
code I have used.
I created a simple 'sin' wave using matplotlib. and a  button. Added
them to a TkFrame.
and then binded the code using

self.f.canvas.mpl_connect('motion_notify_event',_onMotion)

to '_onMotion' function, where I am printing the toooltip.

Can somebody please help me solving the issue.

Code I have used is below.

#!/usr/apps/Python/bin/python
import matplotlib, sys
matplotlib.use('WXAgg')
matplotlib.interactive(False)
import numpy as np
from numpy import arange, sin, pi
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,
NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import wx
import Tkinter
tooltip = wx.ToolTip(tip='tip with a long %s line and a newline\n' %
(' '*100))
 # needs to be added to getover the bug with tooltip.

def alt():
    print 'My Button'

def _onMotion(event):
    print "Xvalue:",event.xdata," Yvalue:",event.ydata
    tip= "Xvalue:{0}, Yvalue: {1}".format(event.xdata,event.ydata)
    tooltip.SetTip(tip)
    tooltip.Enable(True)

class  myClass(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        #global _onMotion
        self.parent=parent

        self.buildFigure()

    def buildFigure(self):

        self.f=plt.figure()
        self.f = plt.figure(figsize=(5,4))
        self.a = self.f.add_subplot(111)
        self.t =
np.array([0.01,0.02,0.03,0.04,0.05,0.07,0.09,1.7,1.9,2.3,2.5,2.7,2.9])
        self.s = sin(2*pi*self.t)
        self.a.plot(self.t,self.s)


        self.dataPlot = FigureCanvasTkAgg(self.f, master=self)
        self.f.canvas.mpl_connect('motion_notify_event',_onMotion)
        self.dataPlot.get_tk_widget().pack(side='top', fill='both')
        self.toolbar = NavigationToolbar2TkAgg( self.dataPlot, self )
        self.toolbar.update()
        self.toolbar.pack()


        self.btn=Tkinter.Button(self, text='btton',command=alt)
        self.btn.pack(ipadx=250)

    def alt (self):
        print 9

if __name__ == "__main__":
    app = myClass(None)
    app.title('Embedding in Tk')
    app.mainloop()




More information about the Python-list mailing list