trouble creating tooltips using Wx in Tk canvas

R

Ravikanth

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

Ian Kelly

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.

That's pretty much what I would expect, since wxPython and Tkinter are
completely different GUI libraries. Trying to use them together is a
bit like trying to call a .NET function from Java -- maybe possible,
but it's going to take a lot of work.
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.

As it says, you haven't created the wx.App object necessary for pretty
much all wxPython code. You could create one, but your tooltip still
would not work correctly because you would be running the Tkinter
event loop rather than the wxPython event loop. If you want to use
the wxToolTip widget, then you should write your program to use
wxPython only. Alternatively, googling for "tkinter tooltip" turns up
a couple of recipes; you could try one of those.

Cheers,
Ian
 
R

Ravikanth

That's pretty much what I would expect, since wxPython and Tkinter are
completely different GUI libraries.  Trying to use them together is a
bit like trying to call a .NET function from Java -- maybe possible,
but it's going to take a lot of work.




As it says, you haven't created the wx.App object necessary for pretty
much all wxPython code.  You could create one, but your tooltip still
would not work correctly because you would be running the Tkinter
event loop rather than the wxPython event loop.  If you want to use
the wxToolTip widget, then you should write your program to use
wxPython only.  Alternatively, googling for "tkinter tooltip" turns up
a couple of recipes; you could try one of those.

Cheers,
Ian

Thank you Ian for your insights to me on this.
I will migrate to wxPython to achieve the functionality.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top