PyQt5入門 PythonでGUI作成 - yu00’s blog
PyQt5ではウェジェットでウィンドウを作成し,
その中にボタンやラベルなどの部品を配置していきます.
ここではウェジェットの基本的な使い方を説明します.
QWidgetを継承したクラスを作る
まずはわかりやすいように,QWidgetを継承した自分用のクラスを作ります.
QWidgetに関する設定はこの継承したクラス内で行います.
#! /usr/bin/python3 # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import QApplication, QWidget # QWidgetを継承したクラスを作る class MyWidget(QWidget): def __init__(self): super().__init__() self.init_ui() # 初期設定 self.show() def init_ui(self): """初期設定を行う""" pass # 後々この部分に初期設定を書く if __name__ == '__main__': app = QApplication(sys.argv) window = MyWidget() # 自分のウィジェットを使う sys.exit(app.exec_())
ウィンドウの表示位置とサイズの変更
QWidget.setGeometry(x, y, w, h) -> none
ウィンドウの表示位置とサイズを変更します
x,y:int, ウィンドウx,y座標
w,h:int, ウィンドウ幅,高さ
ウィンドウタイトルの設定
QWidget.setWindowTitle(title) -> none
ウィンドウタイトルを設定します
サンプルコード
def init_ui(self): self.setWindowTitle('My Title')
実行例
アイコンの設定
QWidget.setWindowIcon(icon) -> none
アイコンを設定します
サンプルコード
def init_ui(self): self.setWindowIcon(QIcon('my-icon.png'))
実行例