#QFontDialog
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class QFontDialogDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle('QFontDialog Demo')

        layout = QVBoxLayout(self)

        self.button1 = QPushButton()
        self.button1.setText('select font')
        self.button1.clicked.connect(self.buttonClick)

        self.label1 = QLabel('test font')

        layout.addWidget(self.button1)
        layout.addWidget(self.label1)
    def buttonClick(self):
        font,bl = QFontDialog.getFont() #打开选择字体对话框 getFont
        if bl:
            self.label1.setFont(font) #设置字体 setFont
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QFontDialogDemo()
    main.show()
    sys.exit(app.exec_())
#QColorDialog
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class QColorDialogDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle('QColorDialogDemo')
        self.resize(300,100)

        layout = QVBoxLayout(self)

        self.button1 = QPushButton()
        self.button1.setText('select color')
        self.button1.clicked.connect(self.buttonClick)

        self.button2 = QPushButton()
        self.button2.setText('select bg color')
        self.button2.clicked.connect(self.button2Click)

        self.label1 = QLabel('test color')
        self.label1.setAlignment(Qt.AlignCenter)

        layout.addWidget(self.button1)
        layout.addWidget(self.button2)
        layout.addWidget(self.label1)
    def buttonClick(self):
        color = QColorDialog.getColor() #打开选择字体对话框 getFont
        p = QPalette() #创建调色板
        p.setColor(QPalette.WindowText,color)#给调色板上色 这里设置调色板的WindowText
        self.label1.setPalette(p)#给label 控件设置调色板

    def button2Click(self):
        color = QColorDialog.getColor() #打开选择字体对话框 getFont
        p = QPalette() #创建调色板
        p.setColor(QPalette.Window,color)#给调色板上色 这里设置调色板的Window
        self.label1.setAutoFillBackground(True) #增加一个自动填充背景色为真
        self.label1.setPalette(p)#给label 控件设置调色板

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QColorDialogDemo()
    main.show()
    sys.exit(app.exec_())
#QFileDialog 演示
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class QFileDialogDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle('QFileDialogDemo')
        self.resize(300,500)

        layout = QVBoxLayout(self)
        self.button1=QPushButton('open image')
        self.button1.clicked.connect(self.loadImage)
        layout.addWidget(self.button1)

        self.label = QLabel('label')
        layout.addWidget(self.label)

        self.button2 = QPushButton('open file')
        self.button2.clicked.connect(self.loadFile)
        layout.addWidget(self.button2)

        self.textEdit = QTextEdit()
        layout.addWidget(self.textEdit)

    def loadImage(self):
        fname,_ = QFileDialog.getOpenFileName(self,'open file','.','image file(*.jpg *.png)')
        self.label.setPixmap(QPixmap(fname))

    def loadFile(self):
        dialog = QFileDialog()
        dialog.setFileMode(QFileDialog.AnyFile)
        dialog.setFilter(QDir.Files)
        if dialog.exec():
            filenames = dialog.selectedFiles()
            f = open(filenames[0],'r')
            with f:
                data = f.read()
                self.textEdit.setText(data)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QFileDialogDemo()
    main.show()
    sys.exit(app.exec_())
posted on 2020-08-27 23:07  94小渣渣  阅读(111)  评论(0编辑  收藏  举报