Is there a way for wxpython to detect javascript mouse events in wx.html2.WebView?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there a way for wxpython to detect javascript mouse events in wx.html2.WebView?

RedHotChiliPepper
I have the following python script

import wx, os
import wx.html2

class MyBrowser(wx.Dialog):
  def __init__(self, *args, **kwds):
    wx.Dialog.__init__(self, *args, **kwds)
    sizer = wx.BoxSizer(wx.VERTICAL)
    self.browser = wx.html2.WebView.New(self)
    sizer.Add(self.browser, 1, wx.EXPAND, 10)
    self.SetSizer(sizer)
    self.SetSize((700, 700))

if __name__ == '__main__':
  app = wx.App()
  dialog = MyBrowser(None, -1)
  dialog.browser.LoadURL(os.path.join(os.getcwd(), 'test.htm'))
  dialog.Show()
  app.MainLoop()

And a sample test.htm script

<!DOCTYPE html>
<html>
<body>
<h1 onclick="alert('hello')">Click here</h1>
</body>
</html>

Is there a way for a python function to be bound to the "click"?


--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way for wxpython to detect javascript mouse events in wx.html2.WebView?

Chris Barker - NOAA Federal
On Fri, Feb 7, 2014 at 1:18 AM, RedHotChiliPepper <[hidden email]> wrote:
I have the following python script
And a sample test.htm script

<!DOCTYPE html>
<html>
<body>
<h1 onclick="alert('hello')">Click here</h1>
</body>
</html>

Is there a way for a python function to be bound to the "click"?

Interesting -- that would be pretty cool.

However, if it's not in place, and if you really want a lot of JS-Python interaction, I'd suggest sticking with the web app paradigm -- i.e. communicate between your python app and the browser window via http in the usual way:

In a separate thread (probably), start up a simple python=based http server ( all the web app frameworks have one -- I'd probably use one of the lighter-weight ones -- tornado, flaks, etc...). You can have it bound to localhost port_something_high. 

Then point html2 at it. You can then use Javascript to make requests in the normal javascript way.

Note that if your web-app component is running in a separate thread, you'll need to use a thread-safe way to call wx code in the main thread -- wx.CallAfter() will suffice for most needs.

-Chris


--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way for wxpython to detect javascript mouse events in wx.html2.WebView?

Rufus V. Smith
In reply to this post by RedHotChiliPepper
Do you have control of the content in the html file?

You can catch clicks by capturing and vetoing a window navigation attempt with
a key string embedded in the location:  With a small change to your html file, and
capturing the EVT_WEB_VIEW_NAVIGATING event, we have this python program:

import wx, os
import wx.html2

class MyBrowser(wx.Dialog):
  def __init__(self, *args, **kwds):
    wx.Dialog.__init__(self, *args, **kwds)
    sizer = wx.BoxSizer(wx.VERTICAL)
    self.browser = wx.html2.WebView.New(self)
    sizer.Add(self.browser, 1, wx.EXPAND, 10)
    self.SetSizer(sizer)
    self.SetSize((700, 700))
    self.Bind(wx.html2.EVT_WEB_VIEW_NAVIGATING, self.OnNavigate,self.browser)

  CATCH_STRING = "catch-this-click:"

  def OnNavigate(self,evt):
    targetUrl=evt.GetURL()
    #print "Navigate Target: ",targetUrl

    if self.CATCH_STRING in targetUrl:
      passed_data = targetUrl.split(self.CATCH_STRING,2)[1]
      print "Click Caught!  Data = ",passed_data
      evt.Veto() # prevent actual navigation.

    else:
      pass # let it do the normal thing...


if __name__ == '__main__':
  app = wx.App()
  dialog = MyBrowser(None, -1)
  url = os.path.join(os.getcwd(), 'test2.htm')
  print "loading url ",url
  dialog.browser.LoadURL(url)
  dialog.Show()
  app.MainLoop()

and this test file test2.htm:

<!DOCTYPE html>
<html>
<body>
<h1 onclick="window.location='catch-this-click:please';alert('hello');">Click here</h1>
</body>
</html>

Will that be enough for your needs?


On Friday, February 7, 2014 4:18:40 AM UTC-5, RedHotChiliPepper wrote:
I have the following python script
...
Is there a way for a python function to be bound to the "click"?

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.