wxPython-Unknown image data format(wxhtml) - python

I want to load a Web page by wxhtml, and use the code to test.
from enter link description here
But I got a problem: Unknown image data format.
import wx
import wx.html
class MyHtmlFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title, size=(600,400))
html = wx.html.HtmlWindow(self)
if "gtk2" in wx.PlatformInfo:
html.SetStandardFonts()
wx.CallAfter(html.LoadPage, "http://www.google.com")
app = wx.PySimpleApp()
frm = MyHtmlFrame(None, "Simple HTML Browser")
frm.Show()
app.MainLoop()

The code you posted worked for me on Xubuntu 14.04 with wxPython 2.8.12.1 and Python 2.7. However, I would highly recommend that you use a different widget. The HtmlWindow is only supposed to be used for basic HTML, not for loading websites. For that, I would recommend using webview, which I believe was added in wxPython 2.9. It would work much better for this sort of thing.

Related

Embedding mayaVI within wxPython

UPDATE: I've placed print commands within the code to isolate the moment of error. The output for the below code is a b c d. I ended up switching to pyqt/pyside using the alternative code provided, but please let me know if I can help in any other way from my current setup in debugging this issue.
from numpy import ogrid, sin
from traits.api import HasTraits, Instance
from traitsui.api import View, Item
from mayavi.sources.api import ArraySource
from mayavi.modules.api import IsoSurface
from mayavi.core.ui.api import SceneEditor, MlabSceneModel
class MayaviView(HasTraits):
scene = Instance(MlabSceneModel, ())
print "a"
view = View(Item('scene', editor=SceneEditor(), resizable=True,
show_label=False),
resizable=True)
print "b"
def __init__(self):
print "z"
HasTraits.__init__(self)
x, y, z = ogrid[-10:10:100j, -10:10:100j, -10:10:100j]
scalars = sin(x*y*z)/(x*y*z)
src = ArraySource(scalar_data=scalars)
self.scene.engine.add_source(src)
src.add_module(IsoSurface())
#-----------------------------------------------------------------------------
# Wx Code
import wx
print "c"
class MainWindow(wx.Frame):
print "d"
def __init__(self, parent, id):
print "e"
wx.Frame.__init__(self, parent, id, 'Mayavi in Wx')
self.mayavi_view = MayaviView()
self.control = self.mayavi_view.edit_traits(
parent=self,
kind='subpanel').control
self.Show(True)
app = wx.PySimpleApp()
frame = MainWindow(None, wx.ID_ANY)
app.MainLoop()
Initial Post:
I'm trying to reproduce the official code on mayaVI's website for embedding in wxWidgets (Wx embedding example).
The first error occurs while using app = wx.PySimpleApp(), and I change it to app = wx.App(False) via an online suggestion.
After this step, the program runs without command line error, but hangs during the data visualization step (the python visualization window never opens up, but does appear as an icon).
To test that my modules were installed correctly, I used MayaVI's official Qt example (Qt embedding example) - and it worked perfectly.
Details: I'm running Pythonw v=2.7.14 within a conda environment, with wxPython v=4.0.1 (osx-cocoa phoenix) and mayaVI v=4.5.0, all via macOS High Sierra Version 10.13.3.
Any advice on this matter would be a huge help - let me know if I can answer anything myself.

How to use Google Chrome as browser in wxpython instead of import wx.lib.iewin

I'm developing a GUI interface using wxpython and I need to load web sites from searches in my GUI using Google Chrome.
I've found the library wx.lib.iewin which makes use of Internet Explorer in the following code:
import wx
import wx.lib.iewin as iewin
class MyBrowser(wx.Dialog):
def __init__(self, *args, **kwds):
wx.Dialog.__init__(self, *args, **kwds)
sizer = wx.BoxSizer(wx.VERTICAL)
self.browser = iewin.IEHtmlWindow(self)
sizer.Add(self.browser, 1, wx.EXPAND, 10)
self.SetSizer(sizer)
self.SetSize((850, 730))
def load(self,uri):
self.browser.Navigate(uri)
if __name__ == '__main__':
app = wx.App()
dialog = MyBrowser(None, -1)
dialog.browser.Navigate("https://www.google.com.mx/maps")
dialog.Show()
app.MainLoop()
Is it a similar library and / or call using Google Chrome instead of IE?
Thanks a lot.
No, that module uses ActiveX to embed the IE browser, but Chrome does not provide an ActiveX control as far as I know.
One possibility would be to use the Chromium Embedded Framework, and the cefpython bindings. I've not used it myself so I do not know how well it works, but there are examples of using it with wxPython. https://github.com/cztomczak/cefpython

Issues with implementing wxpython's html2.WebViewHandler and html2.WebViewFSHandler

I am working on a GUI program which some parts are written in wxpython and some parts in css,html and javascript
The codes below is an example taken from http://wxpython.org/Phoenix/docs/html/MemoryFSHandler.html#memoryfshandler
def OnAbout(self, event):
bcur = wx.BeginBusyCursor()
wx.FileSystem.AddHandler(wx.MemoryFSHandler) #there is a bug here in this example wx.MemoryFSHandler should read wx.MemoryFSHandler()
wx.MemoryFSHandler.AddFile("logo.pcx", wx.Bitmap("logo.pcx", wx.BITMAP_TYPE_PCX))
wx.MemoryFSHandler.AddFile("about.htm",
"<html><body>About: "
"<img src=\"memory:logo.pcx\"></body></html>")
dlg = wx.Dialog(self, -1, _("About"))
topsizer = wx.BoxSizer(wx.VERTICAL)
html = wx.html.HtmlWindow(dlg, size=wx.Size(380, 160), style=wx.HW_SCROLLBAR_NEVER)
html.SetBorders(0)
html.LoadPage("memory:about.htm")
html.SetSize(html.GetInternalRepresentation().GetWidth(),
html.GetInternalRepresentation().GetHeight())
topsizer.Add(html, 1, wx.ALL, 10)
topsizer.Add(wx.StaticLine(dlg, -1), 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 10)
topsizer.Add(wx.Button(dlg, wx.ID_OK, "Ok"),
0, wx.ALL | wx.ALIGN_RIGHT, 15)
dlg.SetAutoLayout(True)
dlg.SetSizer(topsizer)
topsizer.Fit(dlg)
dlg.Centre()
dlg.ShowModal()
wx.MemoryFSHandler.RemoveFile("logo.pcx")
wx.MemoryFSHandler.RemoveFile("about.htm")
These codes shows how to:
to add a MemoryFSHandler and load an HTML string into the memory stream instead of placing html codes in a file and calling that file
Also this example is based on html widget and not webview widget
Below are my codes (trial and error)
class About(wx.Frame):
def __init__(self):
wx.Panel.__init__(self,None,-1,title="This is a working example",size=(700,700))
class Test(wx.Frame):
"""Contact author: contribute a word or send a occurences of bugs"""
def __init__(self,title,pos,size):
wx.Frame.__init__(self,None,-1,title,pos,size)
self.tester=wx.html2.WebView.New(self)
#self.tester.RegisterHandler(wx.html2.WebViewHandler())
wx.FileSystem.AddHandler(wx.MemoryFSHandler())
#self.tester.SetPage("""
wx.MemoryFSHandler().AddFile("about.js","""
document.write("IT is working")
""")
self.tester.LoadURL("memory:about.htm")
I have tried searching the web for some examples but was unfortunate
QUESTION
How can I create a handler for webview widget. This handler should load any html string in a memory stream/file (for example using a URI scheme "memory:.....") so that webview can load an html memory file
Can you post your complete code? Right now you are trying to load
self.tester.LoadURL("memory:about.htm")
But the only memory file you registered was about.js. If you want to reference about.htm you have to register it first:
wx.FileSystem.AddHandler(wx.MemoryFSHandler())
wx.MemoryFSHandler().AddFile("about.js", 'document.write("IT is working")')
wx.MemoryFSHandler().AddFile("about.htm",
"""<html>
<script src="memory:about.js"></script>
<body><h2>It lives!</h2></body>
</html>""")
self.tester.LoadURL("memory:about.htm")
What you need is wx.html2.WebViewFSHandler. I haven't tried this myself, so I'm basing this off the wxWidgets WebView example, but you should be able to do the following after creating your wx.MemoryFSHandler to register the memory handler with the WebView:
self.tester.RegisterHandler(wx.html2.WebViewFSHandler("memory"))
After this, your self.tester.LoadURL("memory:about.htm") call should work.
wx.html2.WebViewFSHandler only exists in Phoenix, though, so if you're not using Phoenix, then I'm afraid your best bet is probably to use WebView's SetPage method instead:
html_data = """<html>
<script>document.write("IT is working");</script>
<body><h2>It lives!</h2></body>
</html>"""
self.tester.SetPage(html_data, "")
EDIT:
I'm adding a complete working example for Phoenix to show how to make it work.
import wx
import wx.html2
class About(wx.Frame):
def __init__(self):
wx.Panel.__init__(self,None,-1,title="This is a working example",size=(700,700))
class Test(wx.Frame):
"""Contact author: contribute a word or send a occurences of bugs"""
def __init__(self,title,pos,size):
wx.Frame.__init__(self,None,-1,title,pos,size)
self.tester=wx.html2.WebView.New(self)
memoryfs = wx.MemoryFSHandler()
wx.FileSystem.AddHandler(memoryfs)
wx.MemoryFSHandler.AddFileWithMimeType("about.js", u'document.write("IT is working")', 'text/plain')
wx.MemoryFSHandler.AddFileWithMimeType("about.htm",
u"""<html>
<script src="memory:about.js"></script>
<body><h2>It lives!</h2></body>
</html>""", 'text/html')
self.tester.RegisterHandler(wx.html2.WebViewFSHandler("memory"))
self.tester.LoadURL("memory:about.htm")
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = Test("Hello", (20, 20), (800, 600))
frame.Show()
app.MainLoop()

WxPython and ShowMeDo tutorial

I was working through the third video in this tutorial series in my effort to learn WxPython. I typed the code as in the video but an error is still returned. I am guessing this has something to do with the WxPython or Python version used. He is using Python 2.4, I am using 2.7.5 and I don't know what WxPython version he is using but I am using 3.0.0.
This is the code:
import wx
class MainWindow(wx.Frame):
def __init__(self,parent,id,title):
wx.Frame.__init__(self,parent,wx.ID_ANY,title,size = (400,200), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE
self.control = wx.TextCtrl(self,1,style = wx.TE_MULTILINE)
self.Show(True)
app = wx.PySimpleApp()
frame = MainWindow(None,-1,"Small Editor")
app.MainLoop()
Invalid syntax is returned for self.control but I don't know why.
Any help is appreciated,
Fluffy
It looks like you're missing the final enclosing parenthesis on the line directly above.
Generally speaking, syntax errors are typically due to misspellings, missing characters, or other such mistakes, and can either be found on the line indicated by the stacktrace or the line directly above.
Once the parenthesis is added, the below code runs for me:
import wx
class MainWindow(wx.Frame):
def __init__(self,parent,id,title):
wx.Frame.__init__(self,parent,wx.ID_ANY,title,size = (400,200), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
self.control = wx.TextCtrl(self,1,style = wx.TE_MULTILINE)
self.Show(True)
app = wx.PySimpleApp()
frame = MainWindow(None,-1,"Small Editor")
app.MainLoop()

wxPython: call a wxApp from another wxApp

Is it possible to run a wxApp from another wxApp?
I am trying to simply call a program I wrote (called DataDeck) from a method of another wxApp, like it was a plugin.
something like:
def on_datadeck_btn_click(self, event):
import datadeck.main
datadeck.main.run()
event.Skip()
where datadeck.main.run() is a classic start of a wxApp:
def run():
app = DataDeck(0)
app.SetAppName("DataDeck")
app.MainLoop()
Right now, it correctly opens DataDeck the first time and it works, but it won't reopen DataDeck a second time after I close it. This would freeze everything.
Update: based on #Mike Driscoll answer, I documented myself more and came to the following solution:
I added an "entry point" in datadeck
def run_as_plugin():
#[do some stuff related to XRC layouts and sysout redirection]
MainGUI = datadeck.gui.maingui.MainGUI()
Where the constructor of MainGUI() automatically shows the wxFrame. Now my application behaves like it was a component of the caller wxApp.
Therefore, I modify the application method as follows:
def on_datadeck_btn_click(self, event):
import datadeck.main
datadeck.main.run_as_plugin()
event.Skip()
It was very simple, indeed! I just had to modify my objects that deal with stdout redirection (not part of this question, I omit the details), and everything worked fine.
There should only be on wx.App. From what I've read online, you can't have two wx.App objects running in one script. You could probably do it using the subprocess module to open a new process though. Take a look at Editra to see some examples for how to do plugins. It is included with wxPython or you can download it separately.
It is perfectly feasible. Not sure why it doesnt work for you.
This example works perfectly:
--main.py--
import wx
class MainFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, title='Main', size=(353,270))
button= wx.Button(self, -1, 'call app', pos=(10,10), size=(-1,30))
self.Bind(wx.EVT_BUTTON, self.capp, button)
def capp(self, event):
import datadeck
datadeck.run()
if __name__ == '__main__':
app = wx.App(0)
frame = MainFrame(None)
frame.Show()
app.MainLoop()
--datadeck.py--
import wx
class DDFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, title='DDFrame', size=(353,270))
button = wx.Button(self, -1, 'print something', pos=(100,100), size=(-1,30))
self.Bind(wx.EVT_BUTTON, self.say_hello, button)
def say_hello(self, event):
print 'something'
class DataDeck(wx.App):
def OnInit(self):
frame = DDFrame(None)
frame.Show()
return True
def run():
app = DataDeck(1)
app.SetAppName("DataDeck")
app.MainLoop()
if you press the 'call app' button you get the new frame open. And you can open as many as you want.
Created aplications/frames are independent of each other. You can close any of them without affecting the others. And the system doesnt freeze.

Categories