0

I am trying to pass the contents of a simple form to the URL parameters on submit of the form. The HTML code works on an online HTML simulator, but doesn't work in wx.html2.WebView. Using wxPython v4 and python 3.6. Code that demonstrates the problem,

import wx
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_WEBVIEW_NAVIGATING, self.OnHTMLNavigate, self.browser)

    def OnHTMLNavigate(self, event):
        targetUrl = event.GetURL()
        print(targetUrl) # format about:<link_info>
        event.Veto()  # stops the link from executing

if __name__ == '__main__':
    app = wx.App()
    dialog = MyBrowser(None, -1)

    text = """
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php" method="get">
  First name: <input type="text" name="fname"><br>
  <input type="submit" value="Submit">
</form>
<p>Click on the submit button, and the input will be sent to a page on the server called "/action_page.php".</p>
</body>
</html>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
"""

    dialog.browser.SetPage(html=text, baseUrl="")
    dialog.Show()
    app.MainLoop()

The output of the program is,

about:/action_page.php

whereas in the html simulator it is,

/action_page.php?fname=myname

The core of what I am trying to do is get information from the html2.WebView back into the python program. The only method I have found is to intercept the navigate event, and parse the URL parameters. I found this method in a post by Rubin.

As a side note, I was able to use a javascript construct to achieve the desired result with buttons, that is, the URL tells me which button was pressed from the webview. Here is a snippet that explains how I did that,

<input id="myButton" type="button" class="button" \
                   onclick="location.href='action/?type=button&name=myButton&cb=myCallback&text=myButtonLabel';" \
                   value="myButtonLabel" />

The button idiom works really well for me because I am able to construct the URL with extra parameters that I need, for example the callback allows me to have each button call a different callback. Ideally I would like to do this with forms too. So if there is another method (javascript?) that I can create the form URL GET submit response with my extra information, and with the text form data, I would be indebted.

DISCLAIMER: I am not a web developer by any stretch. However I must present via webview... I used to use wxPython html window, but graduated to the html2.webview to try to get more functionality.

user9277612
  • 497
  • 4
  • 14

1 Answers1

0

Wasn't able to figure out the problem with the above, but ended up with a final solution, which looks like this,

<form>
  myEnterTextLabel:<br><input type="text" id="myID">
  <input type="button" value="Submit" onclick="text_entry_action('myCh', 'myID', 'myCB')">
  <script type="text/javascript">
    function text_entry_action(ch, id, cb) {
      var input_obj = document.getElementById(id);
      var myurl = ch + "/?type=text_entry&value=" + input_obj.value + "&cb=" + cb;
      window.location.href = myurl;
    }
  </script>
</form>

In the application all the my<> values in the above snippet are programmatically populated before the snippet is sent to the webview.

Then with the OnHTMLNavigate() method above, you intercept the URL, pull out the values, and can run a method from the callback string with the getattr() function. From webview context back to python context.

user9277612
  • 497
  • 4
  • 14