LoginSignup
57
58

More than 5 years have passed since last update.

【PythonでGUI】PyQt5 -始めの一歩-

Last updated at Posted at 2016-05-22

前回の続き

First programs in PyQt5
こちらのサイトを日本語でざっくりとまとめていきます。

【画面表示】

Simple_example.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QApplication, QWidget

if __name__ == '__main__':

    # 必ず作らなければいけないオブジェクト
    app = QApplication(sys.argv)

    # ウィジェットオブジェクトの作成(画面のこと)
    w = QWidget()
    # 画面を横幅を250px、高さを150pxにする
    w.resize(250, 150)
    # x=300,y=300の場所へ画面を移動
    w.move(300, 300)
    # タイトルを設定
    w.setWindowTitle('Simple')
    # 画面表示
    w.show()
    # プログラムをクリーンに終了する
    sys.exit(app.exec_())

simple.png

【アイコンを追加して表示】

An_application_icon.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon


class Example(QWidget):
    # コンストラクタ
    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):

        # setGeometry(x,y,横幅,高さ)
        self.setGeometry(300, 300, 300, 220)
        # タイトルの設定
        self.setWindowTitle('Icon')
        # 画面左上のアイコンを設定
        self.setWindowIcon(QIcon('imoyokan.jpg'))        

        # 画面表示
        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())  

icon.png

【吹き出し】

Showing_a_tooltip.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import (QWidget, QToolTip, 
    QPushButton, QApplication)
from PyQt5.QtGui import QFont    


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):

        # 10pxのサンセリフフォントを吹き出しに使う
        QToolTip.setFont(QFont('SansSerif', 10))

        # 画面の吹き出し設定
        self.setToolTip('This is a <b>QWidget</b> widget')

        # ボタン作り
        btn = QPushButton('Button', self)
        # ボタンの吹き出し設定
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        # ボタンのサイズをいい感じに自動設定
        btn.resize(btn.sizeHint())
        # ボタンの位置設定
        btn.move(50, 50)       

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Tooltips')    
        self.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

tooltips1.pngtooltips2.png

【ボタンで画面を閉じる】

Closing_a_window.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
from PyQt5.QtCore import QCoreApplication


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()        

    def initUI(self):               

        # QPushButtonの第一引数はラベル
        # QPushButtonの第二引数は親ウィジェット(QWidgetに継承されたExampleウィジェット)
        qbtn = QPushButton('Quit', self)
        # Quitボタンをクリックしたら画面を閉じる
        qbtn.clicked.connect(QCoreApplication.instance().quit)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(50, 50)       

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Quit button')    
        self.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

quit.png

【画面を閉じたときの確認画面表示】

Message_Box.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication

class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):               

        self.setGeometry(300, 300, 250, 150)        
        self.setWindowTitle('Message box')    
        self.show()

    # 画面を閉じたときに確認画面を表示するイベントハンドラ    
    def closeEvent(self, event):

        #メッセージ画面の設定いろいろ
        reply = QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QMessageBox.Yes | 
            QMessageBox.No, QMessageBox.No)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()        


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

message.png

【デスクトップウィンドウの真ん中に画面表示】

Centering_window_on_the_screen.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):               

        self.resize(250, 150)
        self.center()

        self.setWindowTitle('Center')    
        self.show()


    def center(self):

        # ウィンドウ全体の情報を取得した長方形qrを作成
        qr = self.frameGeometry()
        # ウィンドウの中心を取得
        cp = QDesktopWidget().availableGeometry().center()
        # 長方形qrをウィンドウの中心へ移動
        qr.moveCenter(cp)
        # 長方形qrの左上と画面の左上を合わせる
        self.move(qr.topLeft())


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())  

次回Menus and toolbarsをざっくり試していきます。

57
58
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
57
58