PyQt5实现Python界面设计_QDialog(对话框)

一、简介

1、对话框QDialog,窗口的一类统称,一般指通过点击触犯弹出的窗口,常见的有消息对话框QMessageBox,颜色对话框QColorDialog,文件对话框QFileDialog,字体对话框QFontDialog等等。

下面举一个例子

'''
对话框:QDialog
QMessageBox
QColorDialog
QFileDialog
QFontDialog
QInputDialog
QMainWindow
QWidget
QDialog
'''
 
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QDialogDemo(QMainWindow):
    def __init__(self):
        super(QDialogDemo,self).__init__()
        self.initUI()
 
    def initUI(self):
        self.setWindowTitle('QDialog案例')
        self.resize(300,200)
 
        self.button = QPushButton(self)
        self.button.setText('弹出对话框')
        self.button.move(50,50)
        self.button.clicked.connect(self.showDialog)
 
    def showDialog(self):
        dialog = QDialog()
        button = QPushButton('确定',dialog)
        button.clicked.connect(dialog.close)
        button.move(50,50)
        dialog.setWindowTitle('对话框')
        dialog.setWindowModality(Qt.ApplicationModal)
 
        dialog.exec()#t弹出对话框
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QDialogDemo()
    main.show()
    app.exec_()
    del app

二、消息对话框QMessageBox

(1)简介

1、消息对话框是对话框的一种,主要功能是作为信息提示使用的,主要有关于对话框,错误对话框,提问对话框,警告对话框,信息对话框等。

2、可以通过不同图标和不同的按钮进行组合

3、官方手册QMessageBox — Qt for Python

PyQt5实现Python界面设计_QDialog(对话框)_第1张图片 

(2)代码实例

'''
消息对话框:QMessageBox
1. 关于对话框
2. 错误对话框
3. 警告对话框
4. 提问对话框
5. 消息对话框
有2点差异
1. 显示的对话框图标可能不同
2. 显示的按钮是不一样的
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QMessageBoxDemo(QWidget):
    def __init__(self):
        super(QMessageBoxDemo,self).__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle('QMessageBox案例')
        self.resize(300,400)
 
        layout = QVBoxLayout()
        self.button1 = QPushButton()
        self.button1.setText('显示关于对话框')
        self.button1.clicked.connect(self.showDialog)
 
        self.button2 = QPushButton()
        self.button2.setText('显示消息对话框')
        self.button2.clicked.connect(self.showDialog)
 
        self.button3 = QPushButton()
        self.button3.setText('显示警告对话框')
        self.button3.clicked.connect(self.showDialog)
 
 
        self.button4 = QPushButton()
        self.button4.setText('显示错误对话框')
        self.button4.clicked.connect(self.showDialog)
 
        self.button5 = QPushButton()
        self.button5.setText('显示提问对话框')
        self.button5.clicked.connect(self.showDialog)
 
 
        layout.addWidget(self.button1)
        layout.addWidget(self.button2)
        layout.addWidget(self.button3)
        layout.addWidget(self.button4)
        layout.addWidget(self.button5)
        self.setLayout(layout)
 
    def showDialog(self):
        text = self.sender().text()
        if text == '显示关于对话框':
            QMessageBox.about(self,'关于','这是一个关于对话框')
        elif text == '显示消息对话框':
            reply = QMessageBox.information(self,'消息','这是一个消息对话框', QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)
            print(reply == QMessageBox.Yes)
        elif text == '显示警告对话框':
            QMessageBox.warning(self,'警告','这是一个警告对话框',QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)
        elif text == '显示错误对话框':
            QMessageBox.critical(self,'警告','这是一个错误对话框',QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)
        elif text == '显示提问对话框':
            QMessageBox.question(self,'警告','这是一个提问对话框',
                                 QMessageBox.Yes | QMessageBox.No|QMessageBox.Ok|QMessageBox.Open|
                                 QMessageBox.Save|QMessageBox.Cancel|QMessageBox.Close|QMessageBox.Discard|
                                 QMessageBox.Apply|QMessageBox.Reset,QMessageBox.Yes)
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QMessageBoxDemo()
    main.show()
    app.exec_()
    del app

 

 三、字体对话框QFontDialog

(1)简介

懂的都懂,使用字体对话框来设置字体,官方手册QFontDialog — Qt for Python

直接上代码

(2)代码演示

'''
字体对话框:QFontDialog
'''
 
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QFontDialogDemo(QWidget):
    def __init__(self):
        super(QFontDialogDemo,self).__init__()
        self.initUI()
 
    def initUI(self):
        self.setWindowTitle('Font Dialog例子')
        layout = QVBoxLayout()
        self.fontButton = QPushButton('选择字体')
        self.fontButton.clicked.connect(self.getFont)
        layout.addWidget(self.fontButton)
 
        self.fontLabel = QLabel('Hello,测试字体例子')
        layout.addWidget(self.fontLabel)
 
        self.setLayout(layout)
    def getFont(self):
        font, ok = QFontDialog.getFont()
        if ok :
            self.fontLabel.setFont(font)#设置字体
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QFontDialogDemo()
    main.show()
    app.exec_()
    del app

 三、颜色对话框

(1)简介

1、使用颜色对话框来获取RGB颜色,官方文档:QColorDialog — Qt for Python

(2)代码演示 

'''
颜色对话框:QColorDialog
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QColorDialogDemo(QWidget):
    def __init__(self):
        super(QColorDialogDemo,self).__init__()
        self.initUI()
 
    def initUI(self):
        self.setWindowTitle('Color Dialog例子')
        layout = QVBoxLayout()
        self.colorButton = QPushButton('设置颜色')
        self.colorButton.clicked.connect(self.getColor)
        layout.addWidget(self.colorButton)
 
        self.colorButton1 = QPushButton('设置背景颜色')
        self.colorButton1.clicked.connect(self.getBGColor)
        layout.addWidget(self.colorButton1)
 
        self.colorLabel = QLabel('Hello,测试颜色例子')
        layout.addWidget(self.colorLabel)
 
        self.setLayout(layout)
    def getColor(self):
        color = QColorDialog.getColor()
        p = QPalette()
        p.setColor(QPalette.WindowText,color)
        self.colorLabel.setPalette(p)
    def getBGColor(self):
        color = QColorDialog.getColor()
        p = QPalette()
        p.setColor(QPalette.Window,color)
        self.colorLabel.setAutoFillBackground(True)
        self.colorLabel.setPalette(p)
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QColorDialogDemo()
    main.show()
    app.exec_()
    del app

四、文件对话框QFileDialog

(1)简介

通过改对话框获取文件的路径,官方文档QFileDialog — Qt for Python

(2)代码演示

 

'''
文件对话框:QFileDialog
'''
 
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QFileDialogDemo(QWidget):
    def __init__(self):
        super(QFileDialogDemo,self).__init__()
        self.initUI()
 
    def initUI(self):
        layout = QVBoxLayout()
        self.button1 = QPushButton('加载图片')
        self.button1.clicked.connect(self.loadImage)
        layout.addWidget(self.button1)
 
        self.imageLabel = QLabel()
        layout.addWidget(self.imageLabel)
 
        self.button2 = QPushButton('加载文本文件')
        self.button2.clicked.connect(self.loadText)
        layout.addWidget(self.button2)
 
        self.contents = QTextEdit()
        layout.addWidget(self.contents)
 
        self.setLayout(layout)
        self.setWindowTitle('文件对话框演示 ')
 
    def loadImage(self):
        fname,_ = QFileDialog.getOpenFileName(self,'打开文件','.','图像文件(*.jpg *.png)')#获取路径
        self.imageLabel.setPixmap(QPixmap(fname))
 
    def loadText(self):
        dialog = QFileDialog()
        dialog.setFileMode(QFileDialog.AnyFile)
        dialog.setFilter(QDir.Files)
 
        if dialog.exec():
            filenames = dialog.selectedFiles()
            f = open(filenames[0],encoding='utf-8',mode='r')
            with f:
                data = f.read()
                self.contents.setText(data)
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QFileDialogDemo()
    main.show()
    app.exec_()
    del app

你可能感兴趣的:(qt,qt,python,开发语言)