QGraphicsScene crashes PyQt4 - python-2.7

Windows XP
Python 2.7
PyQt 4
When I run the following code inside the python interactive shell
from PyQt4 import QtGui
QtGui.QGraphicsScene()
python crashes.
Why?

You need to create a QApplication first before you can use any of the Qt GUI elements.
Usually it's done like this:
from PyQt4 import QtGui
import sys
qapp = QtGui.QApplication(sys.argv)
scene = QtGui.QGraphicsScene()
...
qapp.exec_()

Related

Pyton2.7 GTK webkit enable hardware acceleration

I'm working on some python2.7 widgets for a Digital signage using GTK and webkit and I was wondering How can I enable 2d hardware acceleration?
I'm running Raspbian OS.
import gtk
import webkit
import time
view = webkit.WebView()
sw = gtk.ScrolledWindow()
sw.add(view)
time.sleep(4)
win = gtk.Window(gtk.WINDOW_TOPLEVEL)
win.set_default_size(400, 896)
win.set_decorated(0)
win.add(sw)
win.move(1920, 72)
win.show_all()
view.open("http://yahoo.com")
gtk.main()
Python 3.7 webkit2 was the solution, It looks like the WebKit API for python 2.7 is obsolete.

import Qt ui to Maya2020

Last week I started learning Python for Maya, following the 'CGCircuit - Learn Python Inside Maya'.
Now I'm trying to make a simple UI for Maya2020 using Qt. The tutorial itself is pretty outdated, a lot has changed since Maya2015.
I checked a lot of forums and it seems I’m not the only one having problems. This is what I learned so far:
Qt designer is not a part of Maya anymore, so I downloaded Qt Creator 4.14.0 (Community)
I used pip to install: shiboken2-5.15.2-5.15.2-cp35.cp36.cp37.cp38.cp39-none-win_amd64.whl
If I'm correct, Maya2020 has Pyside2 pr-installed, but I also Installed Pyside2 on Windows10
QtGui.QDialog is replaced by QWidget
Currently I ‘m using Python 2.7.17 on my Windows system, but I also have Python3.8 available in my Environment Variable
When I call the py script inside Maya, like this:
import geomGenerator
reload(geomGenerator)
geomGenerator.getMayaWindow()
geomGenerator.run()
I get this error:
Error: NameError: file C:/Users/12213119/Documents/maya/2020/scripts\geomGenerator.py line 12: global name 'shiboken2' is not defined #
Not sure what this means or how I can solve it. I looked all over the internet, please let me know what I'm doing wrong. Thx in regard.
from PySide2 import QtGui, QtCore, QtUiTools, QtWidgets
from shiboken2 import wrapInstance
import maya.cmds as mc
import maya.OpenMayaUI as omui
def getMayaWindow():
""" pointer to the maya main window
"""
ptr = omui.MQtUtil.mainWindow()
if ptr is not None:
return shiboken2.wrapInstance2(long(ptr), QtWidgets.QWidget)
def run():
""" builds our UI
"""
global win
win = GeometryGenerator(parent=getMayaWindow())
win.show()
class GeometryGenerator(QtWidgets.QWidget):
def __init__(self,parent=None):
super(GeometryGenerator,self).__init__(parent)
I guess you need to run you script with python that comes with Maya, and use pip that comes with maya to install packages.

python 2.7 / pyqt5 and pyinstaller : sip module error

after trying pyqt5 with python3.+ i had some problems with the code than i used python2.7 + pyqt5(for python2.7) and everything worked fine from pycharm and when i run the script from cmd.
now the problem with pyinstaller
pyqt4 / python 2.7 : pyinstaller compile the script and worked fine
pyqt5 / python 2.7 : pyinstaller compile the script but don't work and i capture an error that no module named sip.
the last version of sip 4.19 available only for python3.
if add : import sip to the code i see another error message :
application faild to start because it could not find or load th Qt platform plugin "windows"
all these versions the same problem :
python 2.7.1 / python 2.7.9 / anaconda(python2.7) / winpython(python2.7)
the code :
import sys
import sip
from PyQt5 import QtWidgets
app = QtWidgets.QApplication(sys.argv)
button = QtWidgets.QPushButton("Hello")
button.setFixedSize(100, 50)
button.show()
app.exec_()

error with IPython in showing plots with pyqtgraph

i have successfully installed pyqtgraph library in python 2.7. I forked latest project from GitHub and then python setup.py install. I am now trying to show plots with it. I open a python terminal and start typing the following:-
import pyqtgraph as pg
import numpy as np
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
All of these commands are successfully interpreted.
But then i run the command to see the plot as:-
pg.plot(x, y, symbol='o')
It outputs:
<pyqtgraph.graphicsWindows.PlotWindow at 0x6b7f708>
And then a windows titled pythonw opens and says 'not responding' and hangs and i am unable to see any output. After long time window crashes and terminal says:
Kernel died, restarting
What could be the error? Should i have installed using .exe?
EDIT:
As pointed out below by titusjan, the problem is with the default Jupyter/Ipython notebook shipping with Anaconda which i have not been able to correct. There must be some installation problem. And i am working on Windows.
pyqtgraph plots functions based on PyQT GUI-based programming. So the task of displaying plots have to be treated as starting a GUI. As told above, when I feed the commands to the IPython terminal, it works out fine:
import numpy as np
import pyqtgraph as pg
import sys
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
As noted above, problem starts when i feed the line:
pg.plot(x, y, symbol='o')
To remedy this: either input these 2 lines in one go
pg.plot(x, y, symbol='o')
pg.QtGui.QApplication.exec_()
or immediately after the previous line pg.plot(x, y, symbol='o') input this line:
pg.QtGui.QApplication.exec_()
Alternatively we could use the default QT-GUI commands also. So we get correct plots even if we run this code:-
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import sys
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
pg.plot(x, y, symbol='o')
if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
However, if one wants to avoid invoking QTGui methods explicitly, assuming one has saved the below code as xyz.py, one can run the code successfully displaying the graphics by writing on the command line: pythonw -i xyz.py. This ensures that python has been explicitly asked to run in interactive mode. pythonw is for running in windows.
import numpy as np
import pyqtgraph as pg
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
pg.plot(x, y, symbol='o')

Pygame missing modlues

Im completely new to pygame. after installing it and looking at a tutorial, the first code block i ran (from the tutorial) took me to the pygame modlule, and said that pygame.base could not be found.
it couldn't find any of these at all:
from pygame.base import *
from pygame.constants import *
from pygame.version import *
from pygame.rect import Rect
from pygame.compat import geterror
import pygame.rwobject
import pygame.surflock
import pygame.color
that's from the pygame module.
I looked in both the pygame files that were installed and saw files with those names, though i could not open them (unknown file type)
i installed the python 2.7 binary for windows version of pygame, and used the installer. everything looks like it's in the right place as far as i can tell.
It doesn't seem that you have pygame properly installed. Can you even successfully run import pygame?
Are you sure you only have one copy of python installed?