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

こんばんは。國松です。
 すっかり寒くなり、ダウンジャケットがぴったりな季節になりました。ただ電車の暖房はもう少し弱くてもいいとおもうのですが….
 今回はPyQt5のDialogの2回目です。FotnDialogとFileDialogをみていきます。
参考サイト
 ZetCode PyQt5 tutorial
 【PythonでGUI】PyQt5 -始めの一歩-
3.QFontDialog
 フォントを選択するためのダイアログウィジェットを作成していきます。
 QfontDialogを使用してラベルのフォントを変更できる様にします。
 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #!/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はユーザーがファイルまたはディレクトリを選択できるダイアログです。
 ファイルを開く事も保存することもできます。
 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #!/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_()) | 
※画像や動画などは開けませんのご注意ください。
  

