• python -- PyQt5(designer)中文详细教程(五)对话框


    对话框

    对话框是⼀个现代GUI应用不可或缺的⼀部分。对话是两个人之间的交流,对话框就是⼈与电脑之 间的对话。对话框用来输⼊数据,修改数据,修改应用设置等等。

    输入文字

    QInputDialog 提供了⼀个简单方便的对话框,可以输入字符串,数字或列表。

    1. from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
    2. QInputDialog, QApplication)
    3. import sys
    4. class Example(QWidget):
    5. def __init__(self):
    6. super().__init__()
    7. self.initUI()
    8. def initUI(self):
    9. self.btn = QPushButton('Dialog', self)
    10. self.btn.move(20, 20)
    11. self.btn.clicked.connect(self.showDialog)
    12. self.le = QLineEdit(self)
    13. self.le.move(130, 22)
    14. self.setGeometry(300, 300, 390, 150)
    15. self.setWindowTitle('Input dialog')
    16. self.show()
    17. def showDialog(self):
    18. text, ok = QInputDialog.getText(self, 'Input Dialog',
    19. 'Enter your name:')
    20. if ok:
    21. self.le.setText(str(text))
    22. if __name__ == '__main__':
    23. app = QApplication(sys.argv)
    24. ex = Example()
    25. sys.exit(app.exec_())

    这个示例有⼀个按钮和⼀个输入框,点击按钮显示对话框,输入的文本会显示在输入框⾥。


            text, ok = QInputDialog.getText(self, 'Input Dialog',
                        'Enter your name:')

    这是显示⼀个输入框的代码。第⼀个参数是输⼊框的标题,第⼆个参数是输⼊框的占位符。对话框 返回输⼊内容和⼀个布尔值,如果点击的是OK按钮,布尔值就返回True。

    if ok:
                self.le.setText(str(text))

    把得到的字符串放到输⼊框⾥。

    程序展示:

     

     

     点击OK后:

     

    选取颜色

    QColorDialog提供颜⾊的选择。

    1. from PyQt5.QtWidgets import (QWidget, QPushButton, QFrame,
    2. QColorDialog, QApplication)
    3. from PyQt5.QtGui import QColor
    4. import sys
    5. class Example(QWidget):
    6. def __init__(self):
    7. super().__init__()
    8. self.initUI()
    9. def initUI(self):
    10. col = QColor(0, 0, 0)
    11. self.btn = QPushButton('Dialog', self)
    12. self.btn.move(20, 20)
    13. self.btn.clicked.connect(self.showDialog)
    14. self.frm = QFrame(self)
    15. self.frm.setStyleSheet("QWidget { background-color: %s }"% col.name())
    16. self.frm.setGeometry(130, 22, 100, 100)
    17. self.setGeometry(300, 300, 350, 180)
    18. self.setWindowTitle('Color dialog')
    19. self.show()
    20. def showDialog(self):
    21. col = QColorDialog.getColor()
    22. if col.isValid():
    23. self.frm.setStyleSheet("QWidget { background-color: %s }"% col.name())
    24. if __name__ == '__main__':
    25. app = QApplication(sys.argv)
    26. ex = Example()
    27. sys.exit(app.exec_())

    例子里有⼀个按钮和⼀个 QFrame ,默认的背景颜色为黑色,我们可以使⽤ QColorDialog 改变背景颜色。

            col = QColor(0, 0, 0)

    初始化 QtGui.QFrame 的背景颜色。

            col = QColorDialog.getColor()

    弹出⼀个 QColorDialog 对话框。

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

    我们可以预览颜色,如果点击取消按钮,没有颜⾊值返回,如果颜色是我们想要的,就从取色框里 选择这个颜色。

    程序展示:

    选择字体

            QFontDialog 能做字体的选择。

    1. from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton,
    2. QSizePolicy, QLabel, QFontDialog, QApplication)
    3. import sys
    4. class Example(QWidget):
    5. def __init__(self):
    6. super().__init__()
    7. self.initUI()
    8. def initUI(self):
    9. vbox = QVBoxLayout()
    10. btn = QPushButton('Dialog', self)
    11. btn.setSizePolicy(QSizePolicy.Fixed,QSizePolicy.Fixed)
    12. btn.move(20, 20)
    13. vbox.addWidget(btn)
    14. btn.clicked.connect(self.showDialog)
    15. self.lbl = QLabel('Knowledge only matters', self)
    16. self.lbl.move(130, 20)
    17. vbox.addWidget(self.lbl)
    18. self.setLayout(vbox)
    19. self.setGeometry(300, 300, 350, 180)
    20. self.setWindowTitle('Font dialog')
    21. self.show()
    22. def showDialog(self):
    23. font, ok = QFontDialog.getFont()
    24. if ok:
    25. self.lbl.setFont(font)
    26. if __name__ == '__main__':
    27. app = QApplication(sys.argv)
    28. ex = Example()
    29. sys.exit(app.exec_())

    我们创建了⼀个有⼀个按钮和⼀个标签的 QFontDialog 的对话框,我们可以使用这个功能修改字体样式。

            font, ok = QFontDialog.getFont()

    弹出⼀个字体选择对话框。 getFont() ⽅法返回⼀个字体名称和状态信息。状态信息有OK和其他两 种。

            if ok:
                    self.lbl.setFont(font)

    如果点击OK,标签的字体就会随之更改。

    程序展示:

     

    选择文件

    QFileDialog 给用户提供文件或者文件夹选择的功能。能打开和保存文件。

    1. from PyQt5.QtWidgets import (QMainWindow, QTextEdit,
    2. QAction, QFileDialog, QApplication)
    3. from PyQt5.QtGui import QIcon
    4. import sys
    5. class Example(QMainWindow):
    6. def __init__(self):
    7. super().__init__()
    8. self.initUI()
    9. def initUI(self):
    10. self.textEdit = QTextEdit()
    11. self.setCentralWidget(self.textEdit) # 置中
    12. self.statusBar()
    13. openFile = QAction(QIcon('open.png'), 'Open', self)
    14. openFile.setShortcut('Ctrl+O')
    15. openFile.setStatusTip('Open new File')
    16. openFile.triggered.connect(self.showDialog)
    17. menubar = self.menuBar()
    18. fileMenu = menubar.addMenu('&File')
    19. fileMenu.addAction(openFile)
    20. self.setGeometry(300, 300, 350, 300)
    21. self.setWindowTitle('File dialog')
    22. self.show()
    23. def showDialog(self):
    24. fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
    25. if fname[0]:
    26. f = open(fname[0], 'r')
    27. with f:
    28. data = f.read()
    29. self.textEdit.setText(data)
    30. if __name__ == '__main__':
    31. app = QApplication(sys.argv)
    32. ex = Example()
    33. sys.exit(app.exec_())

    本例中有⼀个菜单栏,⼀个置中的文本编辑框,⼀个状态栏。点击菜单栏选项会弹出⼀ 个 QtGui.QFileDialog 对话框,在这个对话框⾥,你能选择⽂件,然后⽂件的内容就会显示在文本编辑框里。 

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

    这⾥设置了⼀个文本编辑框,文本编辑框是基于 QMainWindow 组件的。

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

    弹出 QFileDialog 窗口。 getOpenFileName() 方法的第⼀个参数是说明文字(就是弹出窗口的标 题),第⼆个参数是默认打开的文件夹路径。默认情况下显示所有类型的文件。

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

    读取选中的文件,并显示在文本编辑框内(但是打开HTML⽂件时,是渲染后的结果,汗)。

    程序展示:

  • 相关阅读:
    从小公司功能测试到一线大厂自动化测试,薪资翻倍,我做到了...
    shell基础
    QML中常见模型使用
    面试总结之JVM入门
    ChatGPT DAN 模式
    【python】JSON标准库文件介绍及python中json模块使用
    AI基础:从线性回归到梯度下降
    tokenizers总结
    Java访问Scala中的Int类型
    [论文笔记]SiameseNet
  • 原文地址:https://blog.csdn.net/weixin_64338372/article/details/128174089