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()
Related
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.
I am trying to utilize pygment for some code highlighting in a wxPython RichTextCtrl.
I can't find much online (other than broken links) about achieving this.
Here is some sample code. I've tried a few different formatters and they all fail. I believe editra using pygment and wxpython, but the source is difficult to navigate.
import wx
import wx.richtext
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters.rtf import RtfFormatter
lexer = get_lexer_by_name("python", stripall=True)
formatter = RtfFormatter()
code = """ # Comment
a = 5
print(a)
print(b)
"""
formatted_code = highlight(code, lexer, formatter)
########################################################################
class MyFrame(wx.Frame):
# ----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, title='Richtext Test')
sizer = wx.BoxSizer(wx.VERTICAL)
self.rt = wx.richtext.RichTextCtrl(self)
self.rt.SetMinSize((300, 200))
self.rt.ChangeValue(formatted_code)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.rt, 1, wx.EXPAND | wx.ALL, 6)
self.SetSizer(sizer)
self.Show()
# ----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
Thanks for any help
I ended up using StyledTextCtrl as suggested in the comments. It turns out there are 2 demos included with the wxPython source, the 2nd of which does exactly what I was trying. I would post the code but it is ~400 lines.
I ran the following python code
import wx
class myframe(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, title="Hello")
self.InitUI()
def InitUI(self):
menubar = wx.MenuBar()
fileMenu = wx.Menu()
qmi = wx.MenuItem(fileMenu, 100, '&Quit\tCtrl+Q')
qmi.SetBitmap(wx.Image(
'quit.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap())
fileMenu.AppendItem(qmi)
self.Bind(wx.EVT_MENU, self.OnQuit, id=100)
menubar.Append(fileMenu, '&File')
self.SetMenuBar(menubar)
self.SetSize((250, 200))
self.SetTitle('Icons and shortcuts')
self.Centre()
self.Show(True)
def OnQuit(self, e):
self.Close()
def main():
ex = wx.App()
myframe()
ex.MainLoop()
if __name__ == '__main__':
main()
The above code is throwing the message
qmi.SetBitmap(wx.Image('quit.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap())
File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py", line 2882, in __init__
_core_.Image_swiginit(self,_core_.new_Image(*args, **kwargs))
PyAssertionError: C++ assertion "strcmp(setlocale(LC_ALL, NULL), "C") == 0" failed at ..\..\src\common\intl.cpp(1449) in wxLocale::GetInfo(): You probably called setlocale() directly instead of using wxLocale and now there is a mismatch between C/C++ and Windows locale.
Things are going to break, please only change locale by creating wxLocale objects to avoid this!
I am new to python and completely unable to solve this.
Is there anyway to find the solution to this.
I am using python 2.7.10 and wxpython 3.0.2.0 in a windows machine.
I had a similar issue that only occurred following a cxFreeze with wxPhoenix. I worried that the solution above could cause issues on a machine that wasn't wx.LANGUAGE_ENGLISH, so instead, I recorded the locale before any imports (which I suspected were changing the locale):
import wx
locale = wx.Locale.GetSystemLanguage()
And then reset it after my imports and after creating the app:
app = wx.App(redirect=False)
app.locale = wx.Locale(locale)
This worked for me.
The solution is to modify the locale and it is working.
Modified the code as here:
def main():
ex = wx.App()
ex.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
myframe()
ex.MainLoop()
Locale is conflicting with system locale. So modified it in the code for this program.
This was fixed if you included some wx.App code:
def InitLocale(self):
import sys
if sys.platform.startswith("win") and sys.version_info > (3, 8):
import locale
locale.setlocale(locale.LC_ALL, "C")
Also, it was fixed correctly in wx4.2 which was just released. If you hit this bug, it was never you. wx and python were disagreeing about the locale. It hits intermittently and only for some non-English locale. 4.2 fixes, or the above code snipple to get rid of the local for post py3.8.
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.
On Linux, how do you disable re-sizing, but at the same time maximize the interface to begin with?
I have tried the following but it is not maximized:
style = wx.DEFAULT_FRAME_STYLE & ~wx.MAXIMIZE_BOX ^ wx.RESIZE_BORDER)
style = wx.MAXIMIZE | wx.DEFAULT_FRAME_STYLE & ~wx.MAXIMIZE_BOX ^ wx.RESIZE_BORDER
EDIT:
Tried Maximize() with resize disabled, still nothing. My versions: Linux, Python 2.4, wxPython 2.8.12.1
(style = wx.MAXIMIZE works for Windows, but not for Linux)
You're pretty close. You just forgot to call Maximize(). See below:
import wx
########################################################################
class NoResizeFrame(wx.Frame):
"""
This frame cannot be resized. It can only be minimized, maximized
and closed
"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
no_resize = wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER |
wx.RESIZE_BOX |
wx.MAXIMIZE_BOX)
wx.Frame.__init__(self, None, title="No Resize", style=no_resize)
panel = wx.Panel(self)
self.Show()
self.Maximize()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = NoResizeFrame()
app.MainLoop()
You might find this tutorial of mine useful for better understanding frame style flags:
http://www.blog.pythonlibrary.org/2013/11/06/wxpython-101-using-frame-styles/
Or this slightly older tutorial on maximizing:
http://www.blog.pythonlibrary.org/2013/07/12/wxpython-making-your-frame-maximize-or-full-screen/