FAQ
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%0)

def alt (self):
print 9

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

Search Discussions

  • Ian Kelly at Jul 6, 2011 at 6:32 pm

    On Wed, Jul 6, 2011 at 9:12 AM, Ravikanth wrote:
    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
  • Ravikanth at Jul 6, 2011 at 7:35 pm

    On Jul 6, 1:32?pm, Ian Kelly wrote:
    On Wed, Jul 6, 2011 at 9:12 AM, Ravikanth wrote:
    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
    Thank you Ian for your insights to me on this.
    I will migrate to wxPython to achieve the functionality.
  • Kevin Walzer at Jul 7, 2011 at 1:04 am

    On 7/6/11 3:35 PM, Ravikanth wrote:

    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.
    Tooltips are trivial to create in Tkinter:

    http://tkinter.unpythonic.net/wiki/ToolTip

    --Kevin

    --
    Kevin Walzer
    Code by Kevin
    http://www.codebykevin.com

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 6, '11 at 3:12p
activeJul 7, '11 at 1:04a
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase