代码之家  ›  专栏  ›  技术社区  ›  Lopoc

PyQt4-未显示小部件

  •  2
  • Lopoc  · 技术社区  · 15 年前

    我用Python和Qt4编写了这个程序。

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    
    
    color = QtGui.QColor(99, 0, 0)
    
    class colorButton(QtGui.QWidget):
        def __init__(self, args):
            QtGui.QWidget.__init__(self,args)
            self.setGeometry(150, 22, 50, 50)
            self.setStyleSheet("QWidget { background-color: %s }" % color.name())
    
    class ColorDialog(QtGui.QWidget):
        def __init__(self, parent=None):
    
            QtGui.QWidget.__init__(self, parent)
    
            self.setGeometry(40, 40, 220, 100)
            self.setWindowTitle('ColorDialog')
    
            button=colorButton(self)
    
    
    app = QtGui.QApplication(sys.argv)
    cd = ColorDialog()
    cd.show()
    app.exec_()
    

    intrpeter没有给我任何错误,但是“彩色”小部件没有显示。为什么? 感谢

    4 回复  |  直到 6 年前
        1
  •  4
  •   Ferdinand Beyer    15 年前

    colorButton QWidget QPushButton.__init__() 在构造函数中。也许你想继承 QPushButton ?

    通过使用以下类定义,您的代码适用于我:

    class colorButton(QtGui.QPushButton):
        def __init__(self, *args):
            QtGui.QPushButton.__init__(self, *args)
            self.setGeometry(150, 22, 50, 50)
            self.setStyleSheet("QWidget { background-color: %s }" % color.name())
    
        2
  •  2
  •   Jesse Aldridge    15 年前

    你需要给小部件一个paintEvent。

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    
    
    color = QtGui.QColor(99, 0, 0)
    
    class colorButton(QtGui.QWidget):
        def __init__(self, args):
            QtGui.QWidget.__init__(self,args)
            self.setGeometry(150, 22, 50, 50)
    
        def paintEvent(self, event):
            painter = QtGui.QPainter(self)
            painter.fillRect(event.rect(), color)
    
    class ColorDialog(QtGui.QWidget):
        def __init__(self, parent=None):
    
            QtGui.QWidget.__init__(self, parent)
    
            self.setGeometry(40, 40, 220, 100)
            self.setWindowTitle('ColorDialog')
    
            button=colorButton(self)
    
    
    app = QtGui.QApplication(sys.argv)
    cd = ColorDialog()
    cd.show()
    app.exec_()
    
        3
  •  0
  •   apt    15 年前

    class colorButton(QtGui.QWidget)
        def __init__(self, args):
            QtGui.QPushButton.__init__(self,args)
            self.setGeometry(150, 22, 50, 50)
    
    
        self.setAutoFillBackground(True)
        plt = QtGui.QPalette()      
        plt.setColor(QtGui.QPalette.Active,QtGui.QPalette.Window,color)
        plt.setColor(QtGui.QPalette.Inactive,QtGui.QPalette.Window,color)  
        plt.setColor(QtGui.QPalette.Disabled,QtGui.QPalette.Window,color
        self.setPalette(plt) 
    
    
        #self.setStyleSheet("QWidget { background-color: %s }" % color.name())
    
        4
  •  0
  •   user44484 user44484    15 年前

    self.setLayout(SOME_LAYOUT)
    

    然后将按钮添加到布局中

    self.layout().addItem(button)
    

    否则,我不确定简单地将ColorDialog作为父级是否足以显示。