简体   繁体   English

PyQt4-小部件未显示

[英]PyQt4 - Widget Is Not Shown

I've made this program in Python and Qt4. 我已经用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_()

The intrpreter doesn't give me any error, but the "colored" widget isn't shown. 解释器不会给我任何错误,但是不会显示“彩色”小部件。 Why? 为什么? thank 谢谢

Your class colorButton inherits from QWidget , yet you are calling QPushButton.__init__() in the constructor. 您的类colorButton继承自QWidget ,但您正在构造函数中调用QPushButton.__init__() Maybe you want it to inherit from QPushButton ? 也许您希望它从QPushButton继承?

By using the following class definition, your code works for me: 通过使用以下类定义,您的代码对我有用:

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())

You need to give the widget a paintEvent. 您需要给小部件一个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_()

Try setting autoFillBackground to True before you change the color (before setStylesheet call). 在更改颜色之前(在setStylesheet调用之前),请尝试将autoFillBackground设置为True。 AND I think you need to set the pallete. 而且我认为您需要设置托盘。 This comment assumes that you meant "the color of the widget is not shown". 此注释假定您的意思是“未显示小部件的颜色”。 Please review the syntax as the one illustrated below is for Qt4.3 and I didn't check the latest one. 请检查语法,因为以下所示的语法适用于Qt4.3,而我没有检查最新的语法。 After you set the pallet, there is no need to set the stylesheet. 设置好货盘后,无需设置样式表。

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())

I think you need to give your ColorDialog a Layout using 我认为你需要给你的ColorDialog一个布局使用

self.setLayout(SOME_LAYOUT)

then add your button to the layout with something like 然后将您的按钮添加到布局中,例如

self.layout().addItem(button)

Otherwise I am not sure if simply giving your button the ColorDialog as parent is sufficient for display. 否则,我不确定是否仅将按钮ColorDialog作为父项就足以显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM