Python PyQt QPainter部件在布局中不显示

2024-09-30 04:41:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python3pyqt GUI开发的新手。我正在尝试制作一个简单的GUI,它在包含许多qhboxlayout的QVBoxLayout的复合布局结构中具有各种小部件。在构建布局时,我使用addStretch()方法来分隔小部件,但是绘制的圆不会显示在GUI显示中。但是,如果我注释掉所有addStretch()函数调用,那么GUI将显示所有需要的小部件(但是间距不好看)。圆圈要去哪里?在

请帮忙!在

不显示圆部件的代码。在

#!/usr/bin/python3

import sys

from PyQt4 import QtCore, QtGui

class DrawCircle(QtGui.QWidget):
    def __init__(self, color, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.color = color

    def setColor(self,color):
        self.color = color

    def paintEvent(self,event):
        self.paint = QtGui.QPainter()
        self.paint.begin(self)
        self.paint.setRenderHint(QtGui.QPainter.Antialiasing)
        radx = 30
        rady = radx
        if self.color == 'red':
            self.paint.setPen(QtCore.Qt.red)
            self.paint.setBrush(QtCore.Qt.red)
        elif self.color == 'green':
            self.paint.setPen(QtCore.Qt.green)
            self.paint.setBrush(QtCore.Qt.green)
        k = int(radx * 1.5)
        center = QtCore.QPoint(k,k)
        self.paint.drawEllipse(center,radx,rady)
        self.paint.end()


class Window(QtGui.QMainWindow, QtGui.QGraphicsView):
    def __init__(self):
        super(Window,self).__init__()

        # GUI Setup
        self.myWid = QtGui.QWidget()

        # Establish a Vertical Box Layout
        # The layout will be a vertical box layout comprised of horizontal
        # box layouts
        self.vbox_fullDisplay = QtGui.QVBoxLayout()


        # Add a Zero-width spacer-item before the area where the
        # text label will appear
        self.vbox_fullDisplay.addStretch(1)

        ###############################
        # Build horizontal box layout #
        ###############################
        self.hbox_top = QtGui.QHBoxLayout()
        # The text should be centered on the screen so zero-width
        # spacer items will be added to the left and right of the label
        self.hbox_top.addStretch(1)

        self.top_text_label = QtGui.QLabel(self)
        self.top_text_label.setFont(QtGui.QFont("Times",32,QtGui.QFont.Bold))
        self.top_text_label.setText("Top of window")
        self.top_text_label.adjustSize()
        self.hbox_top.addWidget(self.top_text_label)

        self.hbox_top.addStretch(1)

        # Add the horizontal box layout to the vertical box layout
        self.vbox_fullDisplay.addLayout(self.hbox_top)

        # Add space between the text and the row of circles
        self.vbox_fullDisplay.addStretch(1)

        ############################
        # Build the row of buttons #
        ############################
        self.hbox_buttons = QtGui.QHBoxLayout()
        # The text should be centered on the screen so zero-width
        # spacer items will be added to the left and right of the label
        self.hbox_buttons.addStretch(1)

        # These buttons will be used to toggle the color of the circles
        btn1 = QtGui.QPushButton('Circle 1')
        btn1.clicked.connect(self.circle1Meth)

        btn2 = QtGui.QPushButton('Circle 2')
        btn2.clicked.connect(self.circle2Meth)

        self.hbox_buttons.addWidget(btn1)
        self.hbox_buttons.addStretch(1)
        self.hbox_buttons.addWidget(btn2)
        self.hbox_buttons.addStretch(1)

        # Add the horizontal box layout to the vertical box layout
        self.vbox_fullDisplay.addLayout(self.hbox_buttons)

        # Add space between the text and the row of circles
        self.vbox_fullDisplay.addStretch(1)

        ############################
        # Build the row of circles #
        ############################
        self.hbox_circles = QtGui.QHBoxLayout()
        self.hbox_circles.addStretch(1)
        self.__circleColors = ['red','green']
        self.__circle1Color = 0;
        self.circle1 = DrawCircle(self.__circleColors[self.__circle1Color])
        print("const draw circ2")
        self.__circle2Color = 1
        self.circle2 = DrawCircle(self.__circleColors[self.__circle2Color])

        self.hbox_circles.addWidget(self.circle1)
        self.hbox_circles.addStretch(1)
        self.hbox_circles.addWidget(self.circle2)
        self.hbox_circles.addStretch(1)

        # Add the row of circles to the vertical box layout
        self.vbox_fullDisplay.addLayout(self.hbox_circles)

        # Add space between the circles and the slot machine pictures
        self.vbox_fullDisplay.addStretch(1)

        self.bottomLabel = QtGui.QLabel(self)
        self.bottomLabel.setText("Bottom Of GUI")
        self.bottomLabel.adjustSize()
        self.vbox_fullDisplay.addWidget(self.bottomLabel)

        self.myWid.setLayout(self.vbox_fullDisplay)
        self.setCentralWidget(self.myWid)

        self.setGeometry(100,200,600,500)
        self.setWindowTitle('Does this work')

    def circle1Meth(self):
        print("Circ1 change")
        if self.__circle1Color == 1:
            self.__circle1Color = 0
        else:
            self.__circle1Color = 1

        # Set the color for circle 1
        self.circle1.setColor(self.__circleColors[self.__circle1Color])

        # force a repaint by updating the widget Using the repaint() method
        # could cause infinite recursion
        self.update()

    def circle2Meth(self):
        print("Circ2 change")
        if self.__circle2Color == 1:
            self.__circle2Color = 0
        else:
            self.__circle2Color = 1

        # Set the color for circle 1
        self.circle2.setColor(self.__circleColors[self.__circle2Color])

        # force a repaint by updating the widget Using the repaint() method
        # could cause infinite recursion
        self.update()

###
# Run GUI
###
app = QtGui.QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())

circle小部件出现的代码(所有addStretch()调用都被注释掉)

^{pr2}$

Tags: ofthetextselftopcolorlayoutpaint
1条回答
网友
1楼 · 发布于 2024-09-30 04:41:06

当您使用addStrech()时,布局将使用sizeHint()作为大小,因为没有设置大小,因此它不可见,解决方案是设置该属性。另一个建议是不要使QPainter成为类的成员,因为它以不可避免的方式消耗内存,只需使其成为局部变量。另一个建议是在setColor()方法中调用update(),它不会为您节省代码行,而且您的代码更简洁。在

class DrawCircle(QtGui.QWidget):
    def __init__(self, color, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.color = color

    def setColor(self,color):
        self.color = color
        self.update()

    def paintEvent(self,event):
        painter = QtGui.QPainter(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        radx = 5
        rady = radx
        if self.color in ('red', 'green'):
            col = QtGui.QColor(self.color)
            painter.setPen(col)
            painter.setBrush(col)
        k = int(radx * 1.5)
        center = QtCore.QPoint(k,k)
        painter.drawEllipse(center,radx,rady)

    def sizeHint(self):
        return QtCore.QSize(15, 15)

相关问题 更多 >

    热门问题