Home >>
Python >> PyQt4 First
■PyQt4の書き方
☆単なるウィンドウを作ってみる
# Simple Window
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.resize(250, 150)
widget.setWindowTitle("simple")
widget.show()
sys.exit(app.exec_())
#Simple Window with Date
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from datetime import date
now = date.today()
message = str(now)
app = QApplication(sys.argv)
label = QLabel("<font color=red size=72><b>"+message+"</b></font>")
label.show( )
dialog = QDialog()
dialog.show()
sys.exit(app.exec_())
☆クラスを継承し、差分でプログラムを作る
# Simple Window with Painting
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class DrawSample(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setGeometry( 30, 30, 300, 300)
self.setWindowTitle("DrawSample")
def paintEvent(self,event):
paint = QPainter()
paint.begin(self)
paint.setPen(Qt.red)
for i in range( 1, 100, 8 ):
paint.drawEllipse(i, i, 100-i*2, 100-i*2)
text = u"サンプル"
paint.setPen(QColor(168, 34, 3))
paint.setFont(QFont('Decorative', 10))
paint.drawText(100, 100, text)
paint.end()
app = QApplication(sys.argv)
ds = DrawSample()
ds.show()
sys.exit(app.exec_())
# Sample Dialog (from "PyQt4 Tutorial")
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class SampleDialog(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setWindowTitle( "Hello Dialog")
title = QLabel( "Title" )
author = QLabel( "Author")
context = QLabel( "Context ")
titleEdit = QLineEdit()
authorEdit = QLineEdit()
conEdit = QTextEdit()
grid = QGridLayout()
grid.setSpacing(10)
grid.addWidget( title, 1, 0 )
grid.addWidget( titleEdit, 1, 1)
grid.addWidget( author, 2, 0)
grid.addWidget( authorEdit, 2, 1)
grid.addWidget( context, 3, 0)
grid.addWidget( conEdit, 3, 1, 5, 1)
self.setLayout(grid)
self.resize(350, 300)
app = QApplication(sys.argv)
sd = SampleDialog()
sd.show()
sys.exit(app.exec_())