其他分享
首页 > 其他分享> > 20.case QDiaglog

20.case QDiaglog

作者:互联网

from PyQt5.QtWidgets import QMainWindow,QWidget,QDialog,QPushButton,QApplication,QDesktopWidget,QHBoxLayout,QVBoxLayout,QFormLayout,QGridLayout,\
    QToolTip,QLabel,QLineEdit,QTextEdit,QRadioButton,QCheckBox,QComboBox,QSlider,QSpinBox,QMessageBox,QInputDialog,\
    QFontDialog,QColorDialog
from PyQt5.QtGui import QIcon,QFont,QPalette,QPixmap,QIntValidator,QDoubleValidator,QRegExpValidator
from PyQt5.QtCore import Qt,QRegExp
import sys

class DialogDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle('DialogDemo')
        self.setGeometry(500,500,300,200)

        button1 = QPushButton('选择颜色')
        button1.clicked.connect(lambda :self.getColor(label_test))
        button2 = QPushButton('选择背景')
        button2.clicked.connect(lambda: self.getColor(label_test))
        button3 = QPushButton('选择字体')
        button3.clicked.connect(lambda: self.getFont(label_test))
        label_test = QLabel('hello python')

        layout_button = QVBoxLayout()
        layout_button.addWidget(button1)
        layout_button.addWidget(button2)
        layout_button.addWidget(button3)
        layout_button.addWidget(label_test)
        self.setLayout(layout_button)

    def getFont(self,label):
        sender = self.sender()
        font, ok = QFontDialog.getFont()
        if ok:
            label.setFont(font)
    def getColor(self,label):
        sender = self.sender()
        p = QPalette()
        label.setAutoFillBackground(True)
        # if sender.text() == '选择颜色':
        #     color1 = QColorDialog.getColor()
        # if sender.text() == '选择背景':
        #     color2 = QColorDialog.getColor()
        p.setColor(QPalette.WindowText, QColorDialog.getColor())
        label.setPalette(p)
        p.setColor(QPalette.Window, QColorDialog.getColor())
        label.setPalette(p)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = DialogDemo()
    main.show()

    sys.exit(app.exec_())


标签:case,__,20,getColor,self,label,QDiaglog,layout,button
来源: https://blog.csdn.net/zmjames2000/article/details/100693719