PyQt5は、(c)はPyQt5基本的なウィジェットをはじめ
、QMainWindow
1、ウィンドウタイプ紹介
QMainWindowは、QWidgetのは、QDialogは、直接使用することができるウィンドウを作成するために使用される、またはあなたが使用することを導き出すことができます。
QMainWindowウィンドウは、メニューバー、ツールバー、ステータスバー、タイトルバーが含まれているウィンドウの中で最も一般的な形式です。
QDialogダイアログウィンドウは、基本クラスは主に、短期の割り当てのために使用され、またはユーザと対話され、それがモーダルかモードレスのいずれかになりますです。QDialogようにダイアログなしメニューバー、ツールバー、ステータスバー、および。
QWidgetのQtはトップレベルウィンドウとして使用することができるグラフィカルコンポーネントの基本クラスであり、それはまた他の構成要素に埋め込むことができます。
2、QMainWindow
QMainWindowはトップレベルのウィンドウで、QMainWindowは独自のレイアウトマネージャを持っている、あなたが使用することはできません、次のようなレイアウトは、設定されsetLayoutの:
メニューバーは、メニューバーで、ツールバーはツールバーで、ドックウィジェットをウィンドウにドッキングされ、中央のウィジェットは、ウィンドウの中心にある、ステータスバーがありますステータスバー。
3、QMainWindow例
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QLabel, QDesktopWidget, QToolBar
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
# 菜单栏设置
self.menuBar = self.menuBar()
self.menuBar.addAction("File")
self.menuBar.addAction("View")
# 工具栏设置
open = QToolBar()
open.addAction("OPen")
self.addToolBar(open)
close = QToolBar()
close.addAction("Close")
self.addToolBar(close)
# 中央组件设置
self.window = QWidget()
self.setCentralWidget(self.window)
# 状态栏设置
self.statusBar = self.statusBar()
self.statusBar.showMessage("This is an status message.", 5000)
label = QLabel("permanent status")
self.statusBar.addPermanentWidget(label)
self.resize(800, 600)
self.setWindowTitle("MainWindow Demo")
self.center()
def center(self):
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
二、QWidgetの
1、QWidgetのプロフィール
QWidgetのは、すべてのウィンドウとコントロールはQWidgetの基本クラスから直接または間接的に継承し、GUIインタフェースのすべてのコンポーネントの基本クラスです。
2、ウィンドウ座標系
:QT統一次のように座標系、ウィンドウコントロールの位置とサイズを見つけるために使用される座標系
Yとして上から下へ、左から右に原点として画面の左上隅に、即ち、(0,0)、X軸正方向を軸正、スクリーンは、トップレベルのウィンドウを配置するための座標系。これは、ウィンドウの独自の内部座標系を有し、ウィンドウを原点と、左上隅の座標系、すなわち、(0,0)、上から下へ、左から右にX軸正方向は、正のY軸であり、原点、X軸、Y軸は、タイトルバー、ボーダーのクライアント領域を中心に、クライアント領域のための領域を同封します。
INTx()constは、
INTY()constは、
INT幅()constは、
int型の高さ()constは、
X()、Y()は、ウィンドウの左上隅の座標、幅()と高さ()クライアント領域を取得する取得します幅と高さ。
ジオメトリ()。X()、ジオメトリ()。yの()は、 クライアント領域の左コーナーを取得し、ジオメトリ()。幅() 、ジオメトリ()。高さ() クライアント領域の幅と高さを取得します。
frameGeometry()X()、 frameGeometry()。Y() クライアント領域の左上隅の座標を取得するために、frameGeometry()幅() 、frameGeometry()。高さ()を含む、クライアント領域、タイトルバー、境界を取得し幅と高さ。
3、QWidgetのインスタンス
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QDesktopWidget, QToolBar
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("MainWidget")
self.resize(800, 600)
self.center()
def center(self):
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
def dispalyGeometry(self):
x = self.x()
y = self.y()
print("x: {0}, y: {1}".format(x, y))
x = self.pos().x()
y = self.pos().y()
print("x: {0}, y: {1}".format(x, y))
x = self.frameGeometry().x()
y = self.frameGeometry().y()
print("x: {0}, y: {1}".format(x, y))
x = self.geometry().x()
y = self.geometry().y()
print("x: {0}, y: {1}".format(x, y))
print("geometry: ", self.geometry())
print("frameGemetry: ", self.frameGeometry())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.show()
window.dispalyGeometry()
sys.exit(app.exec_())
三、QLabel
1、QLabelプロフィール
QLabelは、非編集可能なテキスト、画像、GIFアニメーションを表示するためのプレースホルダとして、QLabelはQFrameから継承されたラベルのようなインターフェース、です。
2、QLabel例
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPalette
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QLabel()
window.setWindowTitle("QLabel Demo")
window.setText("www.baidu.com")
window.setOpenExternalLinks(True)
pallette = QPalette()
pallette.setColor(QPalette.Window, Qt.blue)
window.setPalette(pallette)
window.setAlignment(Qt.AlignCenter)
window.setAutoFillBackground(True)
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
第四に、テキストボックスコントロール
1、のQLineEdit
QLineEdit単一行のテキストボックスコントロール、線列は、ユーザ入力を受信するために、編集することができます。
import sys
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIntValidator
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
label = QLabel("input: ", self)
label.move(0, 0)
lineEdit = QLineEdit(self)
# 设置验证器
intValidator = QIntValidator()
lineEdit.setValidator(intValidator)
lineEdit.setMaxLength(10)
lineEdit.move(50, 0)
lineEdit.textChanged.connect(self.displayText)
def displayText(self, text):
print(text)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
2、QTextEdit
QTextEditは複数行テキスト編集コントロールボックスで、テキストは、表示範囲の制御を超えた場合、テキスト編集の複数の行を編集し、表示することができ、水平および垂直スクロールバーを表示することができ、QTextEditしか表示テキストは、HTMLドキュメントを表示することができますすることはできません。
import sys
from PyQt5.QtWidgets import QApplication, QTextEdit, QWidget, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIntValidator
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
label = QLabel("input: ", self)
label.move(0, 0)
self.textEdit = QTextEdit(self)
self.textEdit.move(50, 0)
self.textEdit.setPlainText("Hello, PyQt5")
self.textEdit.textChanged.connect(self.displayText)
def displayText(self):
print(self.textEdit.toPlainText())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
第五に、ボタンコントロール
1、のQPushButton
QPushButtonはQAbstractButton、長方形、テキスト、矩形領域に表示されるアイコンから継承されました。
import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
button1 = QPushButton("OK", self)
button1.clicked.connect(lambda: self.onClicked(button1))
def onClicked(self, button):
print("Button {0} is clicked.".format(button.text()))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
2、QRadioButton
QRadioButtonは、選択可能なボタンやラベルのセットを提供し、ユーザーがこれらのオプションのいずれか、テキスト情報を表示するためのラベルを選択することができ、QAbstractButtonから継承されました。QRadioButtonはボタンスイッチであり、それがオンまたはオフされ、オンまたはオフに切り替えることができます。ラジオ・グループ、排他的な複数のボタンの唯一のラジオボタンを選択し、必要に応じて組み合わせ、でQGroupBoxまたはQButtonGroupにおいておく必要があります。
import sys
from PyQt5.QtWidgets import QApplication, QRadioButton, QWidget
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
button1 = QRadioButton("OK", self)
button1.toggled.connect(lambda: self.onClicked(button1))
def onClicked(self, button):
print("Button {0} is clicked.status is {1}".format(button.text(), button.isChecked()))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
3、QCheckBox
QCheckBoxは、テキストラベルをチェックボックスのセットを提供する、QAbstractButtonから継承され、ユーザーは複数のオプションを選択することができ、チェックボックス、テキストやアイコンを表示することができます。
半選択、変化がないことを示す:チェックに加えて、未チェック、第3の状態のQCheckBoxesあります。
import sys
from PyQt5.QtWidgets import QApplication, QCheckBox, QWidget
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
button1 = QCheckBox("OK", self)
button1.stateChanged.connect(lambda: self.onStateChanged(button1))
def onStateChanged(self, button):
print("Button {0} is clicked.status is {1}".format(button.text(), button.isChecked()))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
六、QComboBox
QComboBoxは、ドロップダウンリストボックスです。
import sys
from PyQt5.QtWidgets import QApplication, QComboBox, QWidget
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.combo = QComboBox(self)
self.combo.addItem("Apple")
self.combo.addItem("HuaWei")
self.combo.addItem("XiaoMi")
self.combo.addItem("Oppo")
self.combo.currentIndexChanged.connect(self.onCurrentIndex)
def onCurrentIndex(self, index):
print("current item is {0}".format(self.combo.currentText()))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
七、QSpinBox
QSpinBoxカウンタが表示され、ユーザーが編集ボックスから現在の値を入力することができ、現在の増加の値を減らすために上下の矢印ボタンまたはキーボードの上下をクリックすることにより、ユーザーは整数値を選択できるようにするコントロールです。デフォルトによって、QSpinBox 0--99の範囲は、各ステップの値1の変更します。
import sys
from PyQt5.QtWidgets import QApplication, QSpinBox, QWidget
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
spinBox = QSpinBox(self)
spinBox.valueChanged.connect(self.onValueChanged)
def onValueChanged(self, value):
print("current value is {0}".format(value))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
八、QSlider
QSlider制御中に、垂直または水平スライダーを提供し、ユーザが水平または垂直方向に一定の範囲内にスライダを移動することができ、制御値を制御するために制限され、位置スライダー有効範囲内の整数値。
import sys
from PyQt5.QtWidgets import QApplication, QSlider, QWidget
from PyQt5.QtCore import Qt
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
slider = QSlider(Qt.Horizontal, self)
slider.setMaximum(20)
slider.setMinimum(10)
slider.valueChanged.connect(self.onValueChanged)
def onValueChanged(self, value):
print("current value is {0}".format(value))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
九、ダイアログボックスコントロール
1、QDialog
QDialogダイアログクラスは、ウィンドウがsetWindowModalityモーダルウィンドウを提供するメソッドを使用して、三つのモード、非モーダル、モダリティとアプリケーションのモダリティを提供するものです。
(1)非モーダル
非モーダルウィンドウはQt.NonModalセットを使用して、アプリケーションや他の相互作用を×××できます。
(2)モーダルウィンドウ
のウィンドウモードが未処理の現在のダイアログで完了し、Qt.Modalを設定するために使用される相互作用と親ダイアログウィンドウを防ぐことができます。
(3)アプリケーション・モード・
アプリケーション・モードをQt.ApplicationModalを使用し、他のウィンドウとの相互作用を防止します。
ESCキーが押されたときQDialogとその派生クラスのダイアログは、ダイアログウィンドウには、ダイアログボックスを閉じ、デフォルトQDialog.rejectメソッドによって呼び出されます。
setWindowModality()メソッドは、ウィンドウのモーダルウィンドウを提供することができる、何Qtの:: WindowModalityは、Windowsの各show()メソッドを使用して値を、属性ない場合はQt :: WindowModalityのデフォルト値はQt ::非モーダル、表示されているかどうかモードレスウィンドウ。
ユーザが(拒否受け入れられ、2つのスコアを含む)DialogCodeをダイアログボックスを閉じて、返すまでの応答ウィンドウがブロックされている間のダイアログは、exec()メソッドを使用して、モーダルダイアログボックスを表示します。
EXEC()メソッドを使用している場合は何の属性値のQt :: WindowModalityは、アプリケーションレベルのデフォルトのモーダルダイアログのためのダイアログボックスを示していません。表示ウィンドウは、プログラム全体に対応して閉じる前のexec()メソッドを使用してすべてのダイアログがすべてのウィンドウをブロックします。EXEC()メソッドを呼び出した後、ダイアログボックスを閉じるまで、ダイアログボックスブロックが継続されます。承認または拒否のダイアログのexec()メソッドの戻りを閉じた後、対応する動作に応じて一般的な手順は、異なる結果を返します。
モーダルダイアログボックスは、独自のイベントループを持っている、)Qtの:: ApplicationModalためのexecモーダルプロパティを設定し、(ショーを呼び出すための内部()メソッドは、ダイアログボックスを表示し、そして最後にexecメソッド()の終わりを防ぐために、イベントループを有効にします。ウィンドウが閉じられるまで、結果(DialogCodeを)取り戻すイベントループを、最後のexec()メソッドの呼び出しが終了し、EXEC()メソッドの後のコードが続く終了します。
import sys
from PyQt5.QtWidgets import QApplication, QDialog, QWidget, QPushButton
from PyQt5.QtCore import Qt
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
button = QPushButton("OK", self)
self.resize(800, 600)
button.clicked.connect(self.onOKClicked)
def onOKClicked(self):
dialog = QDialog()
dialog.setWindowTitle("Dialog Demo")
dialog.resize(300, 200)
dialog.exec_()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
2、QMessageBox
QMessageBoxが異なるボタンで標準のフィードバックメッセージをクリックするようユーザーに、メッセージを表示できるようにするための汎用ポップアップボックスで、QMessageBoxは、5つの共通ダイアログメッセージの表示方法を提供します。warning(self, QWidget, p_str, p_str_1, buttons, QMessageBox_StandardButtons=None, QMessageBox_StandardButton=None, *args, **kwargs)
警告メッセージの作成]ダイアログボックスcritical(self, QWidget, p_str, p_str_1, buttons, QMessageBox_StandardButtons=None, QMessageBox_StandardButton=None, *args, **kwargs)
重大なエラーメッセージのダイアログボックスを作成するにはinformation(self, QWidget, p_str, p_str_1, buttons, QMessageBox_StandardButtons=None, QMessageBox_StandardButton=None, *args, **kwargs)
、情報メッセージダイアログボックスを作成するためにquestion(self, QWidget, p_str, p_str_1, buttons, QMessageBox_StandardButtons=None, QMessageBox_StandardButton=None, *args, **kwargs)
求めるメッセージボックスを作成するためにabout(self, QWidget, p_str, p_str_1)
情報ダイアログを作成することを
import sys
from PyQt5.QtWidgets import QApplication, QMessageBox, QWidget, QPushButton
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
button = QPushButton("OK", self)
self.resize(800, 600)
button.clicked.connect(self.onOKClicked)
def onOKClicked(self):
button = QMessageBox.question(self, "MessageBox Title", "是否确定关闭?", QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Ok)
if button == QMessageBox.Ok:
print("select Ok Button")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
3、QInputDialog
QInputDialogは(OKとキャンセル)ダイアログコントロール、テキストボックスと2つのボタンである組成物、ユーザがOKボタンをクリック、またはEnterキーを押すことにより、親ウィンドウは、情報入力QInputDialogによって受信することができます。
QInputDialog.getInt制御の標準整数入力へのアクセス
QInputDialog.getItemは、制御入力からオプションのリストを取得する
コントロールからの標準的な文字列入力へQInputDialog.getTextのアクセスは
、標準の浮動小数点コントロールからの入力を取得QInputDialog.getDouble
import sys
from PyQt5.QtWidgets import QApplication, QInputDialog, QWidget, QPushButton
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
button = QPushButton("OK", self)
self.resize(800, 600)
button.clicked.connect(self.onOKClicked)
def onOKClicked(self):
items = ["C++", "Python", "Java", "Go"]
item, ok = QInputDialog.getItem(self, "Select an Item", "Programing Language", items, 0, False)
if ok and item:
print("selected item: ", item)
text, ok = QInputDialog.getText(self, "Input an text", "text:")
if ok:
print("input text: ", text)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
4、QFontDialog
ユーザーが表示されるテキストのフォントサイズ、スタイル、およびフォーマットを選択することができますQFontDialogフォント選択ダイアログ。QFontDialog.getFontは、フォント選択ダイアログから表示フォントサイズ、スタイル、およびフォーマットのテキストを取得することができます。
import sys
from PyQt5.QtWidgets import QApplication, QFontDialog, QWidget, QPushButton
from PyQt5.QtGui import QFont
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
button = QPushButton("OK", self)
self.resize(800, 600)
button.clicked.connect(self.onOKClicked)
def onOKClicked(self):
font, ok = QFontDialog.getFont()
if ok:
print(font.family(), font.pointSize())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
5、QFileDialog
QFileDialogは、標準のファイルを開くで、ファイルを開くとき、ダイアログボックス、QFileDialog使用ファイルフィルタを保存し、表示するファイル名の拡張子を指定します。
import sys
from PyQt5.QtWidgets import QApplication, QFileDialog, QWidget, QPushButton
class MainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
button = QPushButton("OK", self)
self.resize(800, 600)
button.clicked.connect(self.onOKClicked)
def onOKClicked(self):
fname, _ = QFileDialog.getOpenFileName(self, "Open file", '/', "Images(*.jpg *.gif)")
print(fname)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())