【问题标题】:PyQt4 - Widget Is Not ShownPyQt4 - 小部件未显示
【发布时间】:2011-01-01 07:26:24
【问题描述】:

我用 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_()

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

【问题讨论】:

  • class colorButton(QtGui.QWidget) 缺少一个 ':',但这会产生一个 SyntaxError ...
  • 在 Python 中,类名通常以大写字母开头。

标签: python pyqt pyqt4 qwidget


【解决方案1】:

您的类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())

【讨论】:

  • 我已经纠正了在这两种情况下都有对象 QtGui.Qwidget ,但现在!它仍然没有出现
  • 我不知道你在做什么,但是当我在替换 colorButton 定义后运行你的代码时,我看到了棕色按钮。
  • 嗯,它有效,但我不想要 QPushButton。我故意使用 QWidget 对象。
  • 你应该使用super(colorButton, self).__init__(self, *args)
【解决方案2】:

你需要给小部件一个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】:

    在更改颜色之前(在调用 setStylesheet 之前)尝试将 autoFillBackground 设置为 True。我认为你需要设置托盘。此评论假定您的意思是“未显示小部件的颜色”。请检查语法,因为下面说明的语法是针对 Qt4.3 的,我没有检查最新的语法。设置托盘后,无需设置样式表。

    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】:

      我认为您需要使用

      为您的 ColorDialog 提供一个布局
      self.setLayout(SOME_LAYOUT)
      

      然后将您的按钮添加到布局中,例如

      self.layout().addItem(button)
      

      否则,我不确定是否只需将 ColorDialog 作为父级按钮即可显示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-21
        • 2021-05-29
        • 1970-01-01
        • 2019-03-15
        相关资源
        最近更新 更多