大阪市中央区 システムソフトウェア開発会社

営業時間:平日09:15〜18:15
MENU

ラズパイデスクトップでPyQt5入門(11)Dialog in PyQt5(2)

著者:國松亜紗子
公開日:2019/11/29
最終更新日:2019/11/29
カテゴリー:技術情報

こんばんは。國松です。
すっかり寒くなり、ダウンジャケットがぴったりな季節になりました。ただ電車の暖房はもう少し弱くてもいいとおもうのですが….
今回はPyQt5のDialogの2回目です。FotnDialogとFileDialogをみていきます。

参考サイト
ZetCode PyQt5 tutorial
【PythonでGUI】PyQt5 -始めの一歩-

3.QFontDialog
フォントを選択するためのダイアログウィジェットを作成していきます。
QfontDialogを使用してラベルのフォントを変更できる様にします。

#!/usr/bin/python3
#-*= coding: utf-8 -*-

"""
In this example, we select a font name
and change the font of a label.
"""

from PyQt5.QtWidgets import (QWidget,QVBoxLayout,QPushButton,QSizePolicy,QLabel,QFontDialog,QApplication)
import sys

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        vbox = QVBoxLayout()

        btn = QPushButton('Dialog',self)
        btn.setSizePolicy(QSizePolicy.Fixed,
            QSizePolicy.Fixed)

        btn.move(20,20)

        vbox.addWidget(btn)

        btn.clicked.connect(self.showDialog)

        self.lbl = QLabel('Kowledge only matters', self)
        self.lbl.move(130,20)

        vbox.addWidget(self.lbl)
        self.setLayout(vbox)

        self.setGeometry(300,300,250,180)
        self.setWindowTitle('Font dialog')
        self.show()

    def showDialog(self):
        font, ok = QFontDialog.getFont()
        if ok:
            self.lbl.setFont(font)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

 

PyQtにあるFontDialogを呼び出しているだけなのですがなんだかちょっとすごい事をしている気分になれます。あくまで気分だけですが…。

4.FileDialog
FileDialogはユーザーがファイルまたはディレクトリを選択できるダイアログです。
ファイルを開く事も保存することもできます。

#!/usr/bin/pythonn3
#-*- coding; utf-8 -*-

"""
In this example, we select a file with a
QFileDialog and display its contens
in a QTextEdit.
"""

from PyQt5.QtWidgets import (QMainWindow, QTextEdit,QAction,QFileDialog,QApplication)
from PyQt5.QtGui import QIcon
import sys

class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.textEdit = QTextEdit()
        self.setCentralWidget(self.textEdit)
        self.statusBar()
        
        #メニューバーのアイコン設定
        openFile = QAction(QIcon('momo.png'),'Open',self)

        #ショートカット設定
        openFile.setShortcut('Ctrl+O')

        #ステータスバー設定
        openFile.setStatusTip('Opne new File')
        openFile.triggered.connect(self.showDialog)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(openFile)

        self.setGeometry(300,300,350,300)
        self.setWindowTitle('File dialog')
        self.show()

    def showDialog(self):
        fname = QFileDialog.getOpenFileName(self,'Open file','/home')

        if fname[0]:
            f = open(fname[0],'r')

            with f:
                data = f.read()
                self.textEdit.setText(data)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex =  Example()
    sys.exit(app.exec_())

※画像や動画などは開けませんのご注意ください。
 

    上に戻る