from PySide import QtCore, QtGui
import pyqtgraph as pg
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
<span style="color: #008000">self</span><span style="color: #666666">.</span>verticalLayout <span style="color: #666666">=</span> QtGui<span style="color: #666666">.</span>QVBoxLayout(<span style="color: #008000">self</span><span style="color: #666666">.</span>centralwidget)
<span style="color: #008000">self</span><span style="color: #666666">.</span>verticalLayout<span style="color: #666666">.</span>setObjectName(<span style="color: #BA2121">"verticalLayout"</span>)
<span style="color: #008000">self</span><span style="color: #666666">.</span>graph01 <span style="color: #666666">=</span> pg<span style="color: #666666">.</span>PlotWidget(<span style="color: #008000">self</span><span style="color: #666666">.</span>centralwidget)
<span style="color: #008000">self</span><span style="color: #666666">.</span>graph01<span style="color: #666666">.</span>setObjectName(<span style="color: #BA2121">"graph01"</span>)
<span style="color: #008000">self</span><span style="color: #666666">.</span>verticalLayout<span style="color: #666666">.</span>addWidget(<span style="color: #008000">self</span><span style="color: #666666">.</span>graph01)
<span style="color: #008000">self</span><span style="color: #666666">.</span>psbtn <span style="color: #666666">=</span> QtGui<span style="color: #666666">.</span>QPushButton(<span style="color: #008000">self</span><span style="color: #666666">.</span>centralwidget)
<span style="color: #008000">self</span><span style="color: #666666">.</span>psbtn<span style="color: #666666">.</span>setObjectName(<span style="color: #BA2121">"psbtn"</span>)
<span style="color: #008000">self</span><span style="color: #666666">.</span>psbtn<span style="color: #666666">.</span>setText(<span style="color: #BA2121">"Plot"</span>)
<span style="color: #008000">self</span><span style="color: #666666">.</span>verticalLayout<span style="color: #666666">.</span>addWidget(<span style="color: #008000">self</span><span style="color: #666666">.</span>psbtn)
QtCore<span style="color: #666666">.</span>QObject<span style="color: #666666">.</span>connect(<span style="color: #008000">self</span><span style="color: #666666">.</span>psbtn, QtCore<span style="color: #666666">.</span>SIGNAL(<span style="color: #BA2121">"clicked()"</span>), <span style="color: #008000">self</span><span style="color: #666666">.</span>plot)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">plot</span>(<span style="color: #008000">self</span>):
frq <span style="color: #666666">=</span> <span style="color: #666666">10.0</span>
duration <span style="color: #666666">=</span> <span style="color: #666666">1.0</span>
samples <span style="color: #666666">=</span> <span style="color: #666666">1001</span>
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, duration, samples)
rad <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, <span style="color: #666666">2</span> <span style="color: #666666">*</span> np<span style="color: #666666">.</span>pi <span style="color: #666666">*</span> frq, samples)
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sin(rad)
<span style="color: #008000">self</span><span style="color: #666666">.</span>graph01<span style="color: #666666">.</span>plot(x, y)
import sys
import numpy as np
pg.setConfigOption('foreground', 'k')
pg.setConfigOption('background', 'w')
class ControlMainWindow(QtGui.QMainWindow):
def init(self, parent=None):
super(ControlMainWindow, self).init(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if name == "main":
app = QtGui.QApplication(sys.argv)
mySW = ControlMainWindow()#class no instance-ka
mySW.show()
sys.exit(app.exec_())
In other words, in this order
import pyqtgraph as pg
from PySide import QtCore, QtGui
http://www.pyqtgraph.org/documentation/how_to_use.html#pyqt-and-pyside Probably translated.
PyQt and PySide
PyQtGraph supports PyQt and PySide, Python wrappers for two popular QT libraries. Both packages provide almost the same API and functions, but for various reasons, I think you should choose one or the other. When pyqtgraph is first imported, pyqtgraph will automatically determine which library to use with the following check items:
1 If PyQt4 is already imported, use it 2 Otherwise, if PySide is already imported, use it, 3 Otherwise, try to import PyQt4 and 4 If PyQt4 import fails, try importing PySide.
If you have both PyQt4 and PySide libraries installed on your system and want to use pyqtgraph on either side, simply import that library before importing pyqtgraph. please.
import PySide ## this will force pyqtgraph to use PySide instead of PyQt4 import pyqtgraph as pg
The previous error, argument 1 has unexpected type'PySide.QtGui.QWidget'
In the line self.graph01 = pg.PlotWidget (self.centralwidget), pyqtgraph.PlotWidget intended to have something of PyQt in its argument ↓ 'PySide.QtGui.QWidget' has arrived. Argument 1 has an unexpected type'PySide.QtGui.QWidget'. I think that is the state. PyQtGraph A library for plotting in Python. http://www.pyqtgraph.org/
Not bad. Matplotlib is a standard library for plotting in Python. But PyQtGraph is faster. http://yukara-13.hatenablog.com/entry/2013/12/05/025655
If you want to create a GUI using Qt in Python, you can choose between these two. There is a difference between the LGPL and GPL in the license. PySide ・ ・ ・ LGPL. I feel that there is not much information on the net, probably because there are not many people using it. (Approximately 214,000 when searching google) PyQt ・ ・ ・ GPL. Copyleft. I feel that the number of users appears in the search results. (Google search 667,000)
Recommended Posts