Skip to content

Instantly share code, notes, and snippets.

@nickvandewiele
Last active January 31, 2023 19:14
Show Gist options
  • Save nickvandewiele/7cf7ce17b0114b981205d8652d3e821b to your computer and use it in GitHub Desktop.
Save nickvandewiele/7cf7ce17b0114b981205d8652d3e821b to your computer and use it in GitHub Desktop.
PyQT, pyqtgraph, and Cython
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
from window import CustomWindow
app = QtGui.QApplication([])
win = CustomWindow(title="Basic plotting examples")
win.resize(1000,600)
win.setWindowTitle('plot')
# adding a PlotItem to a GraphicsWindow through a constructor, and addItem
title = "Basic array plotting"
p1 = pg.PlotItem(title=title)
win.add(p1)
# adding a data set through PlotDataItem constructor:
d = pg.PlotDataItem(name='data set 1', y=np.random.normal(size=100))
p1.addItem(d)
p1.showAxis('bottom')
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
from setuptools import setup
from setuptools import Extension
from Cython.Build import cythonize
# execute:
# python setup.py build_ext --inplace
setup(
ext_modules = cythonize("window.py")
)
# -*- coding: utf-8 -*-
from pyqtgraph.Qt import QtGui, QtCore
from PyQt4.QtCore import pyqtSlot
import pyqtgraph as pg
class Button(QtGui.QPushButton):
def __init__(self, *args):
super(self.__class__, self).__init__(*args)
self.destroyed.connect(self.doSomeDestruction)
self.clicked.connect(self.onClicked)
# old style signal: old-style slots are always disconnected
# self.connect(self, QtCore.SIGNAL("clicked()"), self.onClicked)
@pyqtSlot()
def onClicked(self):
print 'clicked!'
@pyqtSlot()
def doSomeDestruction(self):
print 'destroy button!'
self.clicked.disconnect()
self.destroyed.disconnect()
class CustomWindow(pg.GraphicsWindow):
def __init__(self, *args, **kwargs):
super(self.__class__, self).__init__(*args, **kwargs)
# create a push button that says OK.
self.btn = Button('OK')
# add QPushButton to the GraphicsWindow:
self.btn.setParent(self)
self.btn.show()
def add(self, plot):
self.plot = plot
self.addItem(plot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment