PyQt4 颜色选择,字体选择代码

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:     buttonTest
   Description :
   Author :       神秘藏宝室
   date:          2017-09-30
-------------------------------------------------
   Change Activity:
                   2017-09-30:
-------------------------------------------------
"""
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from PyQt4 import QtGui,QtCore

class ColorChange(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setGeometry(300, 300, 350, 180)
        self.setWindowTitle(u'调色盘')

        self.color = QtGui.QColor(255,255,255)
        self.button = QtGui.QPushButton(u'选择颜色',self)
        self.button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.button.move(20,20)
        self.connect(self.button,QtCore.SIGNAL('clicked()'),self.showDialog)
        self.setFocus()

        self.widget = QtGui.QWidget(self)
        self.widget.setStyleSheet('QWidget{background-color:%s}'% self.color.name())
        self.widget.setGeometry(130,22,100,100)

    def showDialog(self):
        col= QtGui.QColorDialog.getColor()
        if col.isValid():
            self.widget.setStyleSheet('QWidget{background-color:%s}' % col.name())

class Font(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setGeometry(300, 300, 350, 180)
        self.setWindowTitle(u'字体选择')

        hbox = QtGui.QHBoxLayout()
        self.button = QtGui.QPushButton(u'选择字体',self)
        self.button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.button.move(20,20)
        hbox.addWidget(self.button)
        self.connect(self.button,QtCore.SIGNAL('clicked()'),self.showDialog)

        self.label = QtGui.QLabel(u'测试颜色测试颜色测试颜色测试颜色',self)
        self.label.move(130,20)
        hbox.addWidget(self.label,1)
        self.setLayout(hbox)

    def showDialog(self):
        font,ok=QtGui.QFontDialog.getFont()
        if ok:
            self.label.setFont(font)



app = QtGui.QApplication(sys.argv)
test = Font()
test.show()
sys.exit(app.exec_())
posted on 2017-09-30 23:06  神秘藏宝室  阅读(1038)  评论(0编辑  收藏  举报

 >>>转载请注明出处<<<