目次
PyQt
[Python]
- チュートリアル
- クラスリファレンス
- ドキュメント
- Wiki
インストール
サンプル
ブランクテンプレート
# -*- coding:utf-8 -*- import sys from PyQt4 import QtGui as gui from PyQt4 import QtCore as core class MainWindow(gui.QWidget): def __init__(self): super(MainWindow, self).__init__() self.initUi() def initUi(self): # # self.show() def main(): app = gui.QApplication(sys.argv) mainWin = MainWindow() sys.exit(app.exec_()) if __name__ == '__main__': main()
簡易画面
# -*- coding:utf-8 -*- import sys from PyQt4 import QtGui, QtCore class ExampleQt(QtGui.QWidget): def __init__(self): super(ExampleQt, self).__init__() self.initUi() def getModeItems(self): return (u'Item1',u'Item2',u'Item3') def initUi(self): grid = QtGui.QGridLayout() grid.setSpacing(10) lblMode = QtGui.QLabel(u'テスト') self.cmbMode = QtGui.QComboBox(self) self.cmbMode.addItems(self.getModeItems()) grid.addWidget(lblMode,1,0) grid.addWidget(self.cmbMode,1,1) lblMq = QtGui.QLabel('MQ') self.cmbMqs = QtGui.QComboBox(self) self.cmbMqs.addItems(('Item1','Item2','Item3','Item4','Item5','Item6')) grid.addWidget(lblMq,2,0) grid.addWidget(self.cmbMqs,2,1) self.chkTblPrefix = QtGui.QCheckBox(u'チェックボックス',self) grid.addWidget(self.chkTblPrefix,3,0) boxBtn = QtGui.QBoxLayout(QtGui.QBoxLayout.LeftToRight) self.btnExec = QtGui.QPushButton(u'実行',self) self.btnExec.clicked.connect(self.doExecute) boxBtn.addWidget(self.btnExec) self.btnClear = QtGui.QPushButton(u'クリア',self) self.btnClear.clicked.connect(self.doClear) boxBtn.addWidget(self.btnClear) grid.addLayout(boxBtn, 4,0,1,2) splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal) self.txtData = QtGui.QTextEdit(self) splitter1.addWidget(self.txtData) self.txtOut = QtGui.QTextEdit(self) splitter1.addWidget(self.txtOut) grid.addWidget(splitter1, 5,0,1,5) self.setLayout(grid) self.setGeometry(200, 200, 700, 400) self.show() def doClear(self, value): self.txtData.clear() self.txtOut.clear() def doExecute(self, value): selected = self.getModeItems().index(unicode(self.cmbMode.currentText())) result = '' data = unicode(self.txtData.toPlainText()) if selected == 0: isprefix = self.chkTblPrefix.isChecked() # do something pass elif selected == 1: # do something pass elif selected == 2: # do something pass self.txtOut.clear() self.txtOut.append(result) def main(): app = QtGui.QApplication(sys.argv) exampleQt = ExampleQt() sys.exit(app.exec_()) if __name__ == '__main__': main()
Tips
スクロールバーを先頭に移動
- TextEdit
textEdit.verticalScrollBar().triggerAction(QtGui.QAbstractSlider.SliderToMinimum)
チェックボックスをチェック
from PyQt4 import QtCore as qt chkCountWs = gui.QCheckBox(u"空白計測") chkCountWs.setCheckState(qt.Qt.Checked)
ファイル選択
dialog = QtGui.QFileDialog() dialog.setFileMode(gui.QFileDialog.AnyFile) dialog.setFilter('images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)') if dialog.exec_(): fileNames = dialog.selectedFiles() for f in fileNames: print f
YAGI Hiroto (piroto@a-net.email.ne.jp)
twitter http://twitter.com/pppiroto
Copyright© 矢木 浩人 All Rights Reserved.