Embedding mayaVI within wxPython - python

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.

Related

How to use Pygubu - Python

I am trying to use pygubu from the first time to make better GUI's. I have installed it using pip and it has installed correctly. However when I try and run the example code Here (At the bottom of the web page) I get the error
AttributeError: module 'pygubu' has no attribute 'Builder'
I don't know if this code is correct or not. I have looked for ways to use this tool but all I can find are links and videos for installing it. I have also tried This video but can't figure out how to open/run/use. I am using python-idle if that is the issue. The code (if you don't want to follow the link) is:
# helloworld.py
import tkinter as tk
import pygubu
class HelloWorldApp:
def __init__(self):
#1: Create a builder
self.builder = builder = pygubu.Builder()
#2: Load an ui file
builder.add_from_file('helloworld.ui')
#3: Create the mainwindow
self.mainwindow = builder.get_object('mainwindow')
def run(self):
self.mainwindow.mainloop()
if __name__ == '__main__':
app = HelloWorldApp()
app.run()
I would appreciate any help. Also when I try installing it again - just to check - I get:
Pygubu is an application. Find it in files from C: drive. The code:
# helloworld.py
import tkinter as tk
import pygubu
class HelloWorldApp:
def __init__(self):
#1: Create a builder
self.builder = builder = pygubu.Builder()
#2: Load an ui file
builder.add_from_file('helloworld.ui')
#3: Create the mainwindow
self.mainwindow = builder.get_object('mainwindow')
def run(self):
self.mainwindow.mainloop()
if __name__ == '__main__':
app = HelloWorldApp()
app.run()
Is run after you have created your UI (The format that this application uses) and that the name of the UI specified is helloworld.ui.
Note that instead of helloworld.ui in the following line:
builder.add_from_file('helloworld.ui') You should insert the filename
(or path) of your just saved UI definition.
Note also that instead of 'mainwindow' in the following line:
self.mainwindow = builder.get_object('mainwindow') You should have the
name of your main widget (the parent of all widgets), otherwise you
will get an error similar to the following:
Exception: Widget not defined.
This link explains the usage better than one stated in question

PyQt5: Looking for example of Embedding external app window in Application

I have a largeish application I'm developing in Python3 using PyQt5. I want to do something very much like what is being done here in PyQt4:
# -*- coding: utf-8 -*-
import atexit
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class XTerm(QX11EmbedContainer):
def __init__(self, parent, xterm_cmd="xterm"):
QX11EmbedContainer.__init__(self, parent)
self.xterm_cmd = xterm_cmd
self.process = QProcess(self)
self.connect(self.process,
SIGNAL("finished(int, QProcess::ExitStatus)"),
self.on_term_close)
atexit.register(self.kill)
def kill(self):
self.process.kill()
self.process.waitForFinished()
def sizeHint(self):
size = QSize(400, 300)
return size.expandedTo(QApplication.globalStrut())
def show_term(self):
args = [
"-into",
str(self.winId()),
"-bg",
"#000000", # self.palette().color(QPalette.Background).name(),
"-fg",
"#f0f0f0", # self.palette().color(QPalette.Foreground).name(),
# border
"-b", "0",
"-w", "0",
# blink cursor
"-bc",
]
self.process.start(self.xterm_cmd, args)
if self.process.error() == QProcess.FailedToStart:
print "xterm not installed"
def on_term_close(self, exit_code, exit_status):
print "close", exit_code, exit_status
self.close()
(from https://bitbucket.org/henning/pyqtwi...11/terminal.py)
which is: Embed an X11 shell terminal (xterm) into a tab of my application.
I understand that QX11EmbedContainer is not in PyQt5, and that some usage of createWindowContainer may be the new approach, but I cannot find any example of this in action to follow.
I would ideally like to see the above code ported to a fully functional example using PyQt5 conventions and methods, but any assistance in any form will be helpful.
I have looked at Embedding external program inside pyqt5, but there is only a win32 solution there, and I need to run on Linux and MacOS, with windows being an unnecessary nicety, if there was a generalized solution to fit all 3. It feels like this is close, but I cannot find the appropriate replacement for the win32gui module to use in a general implementation.
I have looked at example of embedding matplotlib in PyQt5 1, but that pertains to matplotlib in particular, and not the general case.
All pointers and help will be gratefully acknowledged.

UI made in QT Designer shifts behind Title Bar [duplicate]

I'm trying to create an application that contains a web browser within it, but when I add the web browser my menu bar visually disappears but functionally remains in place. The following are two images, one showing the "self.centralWidget(self.web_widget)" commented out, and the other allows that line to run. If you run the example code, you will also see that while visually the entire web page appears as if the menu bar wasn't present, you have to click slightly below each entry field and button in order to activate it, behaving as if the menu bar was in fact present.
Web Widget Commented Out
Web Widget Active
Example Code
import os
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *
class WebPage(QWebEngineView):
def __init__(self, parent=None):
QWebEngineView.__init__(self)
self.current_url = ''
self.load(QUrl("https://facebook.com"))
self.loadFinished.connect(self._on_load_finished)
def _on_load_finished(self):
print("Url Loaded")
class MainWindow(QMainWindow):
def __init__(self, parent=None):
# Initialize the Main Window
super(MainWindow, self).__init__(parent)
self.create_menu()
self.add_web_widet()
self.show()
def create_menu(self):
''' Creates the Main Menu '''
self.main_menu = self.menuBar()
self.main_menu_actions = {}
self.file_menu = self.main_menu.addMenu("Example File Menu")
self.file_menu.addAction(QAction("Testing Testing", self))
def add_web_widet(self):
self.web_widget = WebPage(self)
self.setCentralWidget(self.web_widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.showMaximized()
sys.exit(app.exec_()) # only need one app, one running event loop
Development Environment
Windows 10, PyQt5, pyqt5-5.9
EDIT
The problem doesn't seem to be directly related to the menu bar. Even removing the menu bar the issue still occurs. That said, changing from showMaximized() to showFullScreen() does seem to solve the problem.
I no longer believe this is an issue with PyQt5 specifically but rather a problem with the graphics driver. Specifically, if you look at Atlassian's HipChat application it has a similar problem which is documented here:
https://jira.atlassian.com/browse/HCPUB-3177
Some individuals were able to solve the problem by running the application from the command prompt with the addendum "--disable-gpu" but that didn't work for my python application. On the other hand, rolling back the Intel(R) HD Graphics Driver did solve my problem. Version 21.20.16.4627 is the one that seems to be causing problems.

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()

Modal dialog freezes the whole application

I use wxpython altogether with matplotlib backend on an ubuntu machine. I would like to connect my matplotlib canvas to a button_press_event that pops up a wxpython modal dialog. When the modal dialog pops up, the whole application gets frozen. This problem does not occur on a windows machine. Here is a snippet that typically reproduces the problem.
import wx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.figure import Figure
class SettingDialog(wx.Dialog):
def __init__(self, parent=None):
wx.Dialog.__init__(self, parent, wx.ID_ANY, title="Modal dialog")
class PlotterFrame(wx.Frame):
def __init__(self, parent, title="Frame with matplotlib canvas"):
wx.Frame.__init__(self, parent, wx.ID_ANY, title)
self.figure = Figure(figsize=(5,4), dpi=None)
self.canvas = FigureCanvasWxAgg(self, -1, self.figure )
self.canvas.mpl_connect("button_press_event", self.on_click)
def on_click(self, event=None):
d = SettingDialog(self)
d.ShowModal()
d.Destroy()
if __name__ == "__main__":
app = wx.App(False)
f = PlotterFrame(None)
f.Show()
app.MainLoop()
Would you have any idea of what is wrong with my code ?
PS0 : The problem is that the dialog window is also frozen, like all the applications in the desktop wich do not react anymore. the only way to escape is to switch to another desktop using the keyboard
PS1 : with a very common example like http://eli.thegreenplace.net/files/prog_code/wx_mpl_bars.py.txt
the problem also appear, I conclude so that, this issue is a bug on linux (here ubuntu 12.04) for the following libs version :
wx.version : '2.8.12.1'
matplotlib.version : '1.1.1rc'
The whole point of a modal dialog is that it freezes the application while the dialog is in its modal state. If you don't want the application to freeze, then don't show the dialog modally.
I ran into this problem too, on several different Linux systems. None of the various mentioned resources seem to be describing exactly the same as this problem. After some investigation, it seems that something is locking up when you try to show a modal dialog before the mouse release event fires in the Matplotlib FigureCanvas.
Once I realized that, the solution is very simple. Your event handler should become:
def on_click(self, event=None):
try:
event.guiEvent.GetEventObject().ReleaseMouse()
except:
pass
d = SettingDialog(self)
d.ShowModal()
d.Destroy()
One issue that could complicate the code is that not all matplotlib events have the same structure. So if this had been a 'pick_event' handler, you would instead do
event.mouseevent.guiEvent.GetEventObject().ReleaseMouse()
Check http://matplotlib.org/users/event_handling.html for the key to which Event types are passed in by which matplotlib events.

Categories