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_())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值