1. 程式人生 > >PyQt5入門(四)——對話方塊

PyQt5入門(四)——對話方塊

此總結主要參考下面這篇文章:PyQt5對話方塊

例子1、2、3的self繼承自QWidget

1. QInputDialog

from PyQt5.QtWidgets import QPushButton, QLineEdit, QInputDialog

# 這裡只匯入與示例相關的必要的類

self.btn = QPushButton('Dialog', self)
self.btn.move(20, 20)
self.btn.clicked.connect(self.showDialog)

self.le = QLineEdit(self)
self.le.move(130
, 22) def showDialog(self): text, ok = QInputDialog.getText(self, 'Input Dialog', 'Enter your name:') if ok: self.le.setText(str(text))

2. QColorDialog

from PyQt5.QtWidgets import QPushButton, QFrame, QColorDialog
from PyQt5.QtGui import QColor

col = QColor(0, 0, 0)

self.btn = QPushButton('Color'
, self) self.btn.move(20, 60) self.btn.clicked.connect(self.showColor) self.frm = QFrame(self) self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name()) self.frm.setGeometry(130, 62, 100, 100) def showColor(self): col = QColorDialog.getColor() if col.isValid(): self.frm.setStyleSheet("QWidget { background-color: %s }"
% col.name())

3. QFontDialog & QLabel

from PyQt5.QtWidgets import QPushButton, QSizePolicy, QLabel, QFontDialog

self.btn = QPushButton('Dialog', self)
self.btn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.btn.move(20, 140)
self.btn.clicked.connect(self.showFont)

self.lbl = QLabel('Knowledge only matters', self)
self.lbl.move(130, 142)

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

綜合上述功能,得到如下示例:

import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit, QFrame, QColorDialog, QInputDialog, 
QApplication, QSizePolicy, QLabel, QFontDialog)
from PyQt5.QtGui import QColor

class Example(QWidget):

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

        self.initUI()

    def initUI(self):
        col = QColor(0, 0, 0)

        self.btn1 = QPushButton('Dialog', self)
        self.btn1.move(20, 20)
        self.btn1.clicked.connect(self.showDialog)

        self.btn2 = QPushButton('Color', self)
        self.btn2.move(20, 60)
        self.btn2.clicked.connect(self.showColor)

        self.btn3 = QPushButton('Font', self)
        self.btn3.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.btn3.move(20, 180)
        self.btn3.clicked.connect(self.showFont)

        self.le = QLineEdit(self)
        self.le.setGeometry(130, 22.5, 100, 20)

        self.frm = QFrame(self)
        self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name())
        self.frm.setGeometry(130, 62, 100, 100)

        self.lbl = QLabel('Knowledge only matters', self)
        self.lbl.setGeometry(130, 180, 200, 30)

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

    def showDialog(self):
        [text, ok] = QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
        if ok:
            self.le.setText(str(text))

    def showColor(self):
        col = QColorDialog.getColor()
        if col.isValid():
            self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name())

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

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

例子1、2、3的self繼承自QMainWindow

4. QFileDialog

from PyQt5.QtWidgets import QTextEdit, QAction, QFileDialog

self.textEdit = QTextEdit()
self.setCentralWidget(self.textEdit)

openFile = QAction('Open', self)
openFile.setShortcut('Ctrl+O')
openFile.triggered.connect(self.showDialog)

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

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

    if fname[0]:
        f = open(fname[0], 'r')
        with f:
            data = f.read()
            self.textEdit.setText(data)

完整示例:

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


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('web.jpg'), 'Open', self)
        openFile.setShortcut('Ctrl+O')
        openFile.setStatusTip('Open 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','C:/users/admin/desktop/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_())