PyQtとは、GUIツールキットQtのPythonバインドです。
Windows, MacOS X, Linux など多くのOS上で動作します。
ここで使用しているモジュールは以下の通りです。
PyQt4.QtCore
PyQt4.QtGui
PyQt4.QtOpenGL
#!env python
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.resize(300, 300)
widget.setWindowTitle('PyQt 1')
widget.show()
sys.exit(app.exec_())
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.resize(300, 300)
widget.setWindowTitle('PyQt 1')
widget.show()
sys.exit(app.exec_())
#!env python
import sys
from PyQt4 import QtGui, QtCore
class QuitButton(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(50, 50, 300, 300)
self.setWindowTitle('Close button')
cb = QtGui.QPushButton('Close', self)
cb.setGeometry(10, 10, 60, 35)
cb.clicked.connect(QtGui.qApp.quit)
app = QtGui.QApplication(sys.argv)
b = QuitButton()
b.show()
sys.exit(app.exec_())
from PyQt4 import QtGui, QtCore
class QuitButton(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(50, 50, 300, 300)
self.setWindowTitle('Close button')
cb = QtGui.QPushButton('Close', self)
cb.setGeometry(10, 10, 60, 35)
cb.clicked.connect(QtGui.qApp.quit)
b = QuitButton()
b.show()
#!env python
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class sliderWindow(QtGui.QWidget):
def __init__(self):
super(sliderWindow, self).__init__()
slider = QtGui.QSlider(QtCore.Qt.Horizontal, self)
slider.setFocusPolicy(QtCore.Qt.NoFocus)
slider.setGeometry(30, 40, 100, 30)
slider.valueChanged.connect(self.changeValue)
self.label = QtGui.QLabel(self)
self.label.setText('0')
self.label.setGeometry(160, 40, 80, 30)
self.setWindowTitle('Slider')
self.setGeometry(50, 50, 300, 300)
def changeValue(self, value):
self.label.setText(str(value))
app = QtGui.QApplication(sys.argv)
s = sliderWindow()
s.show()
sys.exit(app.exec_())
from PyQt4 import QtGui
from PyQt4 import QtCore
class sliderWindow(QtGui.QWidget):
def __init__(self):
super(sliderWindow, self).__init__()
slider = QtGui.QSlider(QtCore.Qt.Horizontal, self)
slider.setFocusPolicy(QtCore.Qt.NoFocus)
slider.setGeometry(30, 40, 100, 30)
slider.valueChanged.connect(self.changeValue)
self.label = QtGui.QLabel(self)
self.label.setText('0')
self.label.setGeometry(160, 40, 80, 30)
self.setWindowTitle('Slider')
self.setGeometry(50, 50, 300, 300)
def changeValue(self, value):
self.label.setText(str(value))
PyQtでOpenGLの描画をするには、QGLWidgetを継承したクラスを作成し、 その中で設定•描画します。
initializeGL(self)
paintGL(self)
resizeGL(self, w, h)
updateGL()
#!env python
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from PyQt4 import QtGui
from PyQt4.QtOpenGL import *
class QTGLWidget1(QGLWidget):
vertex = [
[ 0.0, 0.0, 0.0 ],
[ 1.0, 0.0, 0.0 ],
[ 1.0, 1.0, 0.0 ],
[ 0.0, 1.0, 0.0 ],
[ 0.0, 0.0, 1.0 ],
[ 1.0, 0.0, 1.0 ],
[ 1.0, 1.0, 1.0 ],
[ 0.0, 1.0, 1.0 ]]
edge = [
[ 0, 1 ],
[ 1, 2 ],
[ 2, 3 ],
[ 3, 0 ],
[ 4, 5 ],
[ 5, 6 ],
[ 6, 7 ],
[ 7, 4 ],
[ 0, 4 ],
[ 1, 5 ],
[ 2, 6 ],
[ 3, 7 ]]
def __init__(self, parent):
QGLWidget.__init__(self, parent)
self.setMinimumSize(300, 300)
def paintGL(self):
glClearColor(0.0, 0.0, 1.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
glBegin(GL_LINES)
for i in range(0, 12):
glVertex(self.vertex[self.edge[i][0]])
glVertex(self.vertex[self.edge[i][1]])
glEnd()
glFlush()
def resizeGL(self, w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(30.0, w/h, 1.0, 100.0)
glMatrixMode(GL_MODELVIEW)
def initializeGL(self):
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glClearColor(0.0, 0.0, 0.0, 1.0)
glClearDepth(1.0)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(40.0, 1.0, 1.0, 30.0)
class QTGLSample(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
widget = QTGLWidget1(self)
self.setCentralWidget(widget)
app = QtGui.QApplication(sys.argv)
window = QTGLSample()
window.setWindowTitle('PyQt OpenGL 1')
window.show()
sys.exit(app.exec_())
from PyQt4.QtOpenGL import *
class QTGLWidget1(QGLWidget):
def __init__(self, parent):
QGLWidget.__init__(self, parent)
self.setMinimumSize(300, 300)
def paintGL(self):
def resizeGL(self, w, h):
def initializeGL(self):
class QTGLSample(QtGui.QMainWindow):
QtGui.QMainWindow.__init__(self)
widget = QTGLWidget1(self)
self.setCentralWidget(widget)
レイアウトの作成•設定をするためのメソッドです。
QtGui.QHBoxLayout()
QtGui.QVBoxLayout()
addWidget(widget)
addLayout(layout)
setLayout(layout)
#!env python
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtOpenGL import *
class QTGLWidget2(QGLWidget):
vertex = [
[ 0.0, 0.0, 0.0 ],
[ 1.0, 0.0, 0.0 ],
[ 1.0, 1.0, 0.0 ],
[ 0.0, 1.0, 0.0 ],
[ 0.0, 0.0, 1.0 ],
[ 1.0, 0.0, 1.0 ],
[ 1.0, 1.0, 1.0 ],
[ 0.0, 1.0, 1.0 ]]
edge = [
[ 0, 1 ],
[ 1, 2 ],
[ 2, 3 ],
[ 3, 0 ],
[ 4, 5 ],
[ 5, 6 ],
[ 6, 7 ],
[ 7, 4 ],
[ 0, 4 ],
[ 1, 5 ],
[ 2, 6 ],
[ 3, 7 ]]
def __init__(self, parent):
QGLWidget.__init__(self, parent)
self.setMinimumSize(300, 300)
self.br = 0.0
self.bg = 0.0
self.bb = 1.0
def paintGL(self):
glClearColor(self.br, self.bg, self.bb, 0.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
glBegin(GL_LINES)
for i in range(0, 12):
glVertex(self.vertex[self.edge[i][0]])
glVertex(self.vertex[self.edge[i][1]])
glEnd()
glFlush()
def resizeGL(self, w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(30.0, w/h, 1.0, 100.0)
glMatrixMode(GL_MODELVIEW)
def initializeGL(self):
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glClearColor(self.br, self.bg, self.bb, 1.0)
glClearDepth(1.0)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(40.0, 1.0, 1.0, 30.0)
def changeBackColor(self):
c = self.br
self.br = self.bg
self.bg = self.bb
self.bb = c
self.updateGL()
class QTWidget(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.gl = QTGLWidget2(self)
bb = QtGui.QPushButton('Back Color', self)
bb.clicked.connect(self.gl.changeBackColor)
cb = QtGui.QPushButton('Close', self)
cb.clicked.connect(QtGui.qApp.quit)
hbox = QtGui.QHBoxLayout()
hbox.addWidget(bb)
hbox.addWidget(cb)
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.gl)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.resize(300, 350)
app = QtGui.QApplication(sys.argv)
w = QTWidget()
w.setWindowTitle('PyQt OpenGL 2')
w.show()
sys.exit(app.exec_())
self.br = 0.0
self.bg = 0.0
self.bb = 1.0
def changeBackColor(self):
c = self.br
self.br = self.bg
self.bg = self.bb
self.bb = c
self.updateGL()
class QTWidget(QtGui.QWidget):
self.gl = QTGLWidget2(self)
bb = QtGui.QPushButton('Back Color', self)
bb.clicked.connect(self.gl.changeBackColor)
cb = QtGui.QPushButton('Close', self)
cb.clicked.connect(QtGui.qApp.quit)
hbox = QtGui.QHBoxLayout()
hbox.addWidget(bb)
hbox.addWidget(cb)
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.gl)
vbox.addLayout(hbox)
self.setLayout(vbox)