每次按下按钮时都会更改 QLabel,它会被覆盖

Changing QLabel on a push of a button each time its pressed it's overwrited

我想打印出用户输入的一组 3 个数字的平均值。但是,每次我按下按钮时,文本都会相互重叠

def Comp
    Average = QtGui.QLabel("The Students Average is " + str(self.average), self)
    Average.move(400,300)
    Average.setFont(QtGui.QFont('sanSerif',16))
    Average.resize(Average.sizeHint())
    Average.show()

btn = QtGui.QPushButton("Test",self)
btn.clicked.connect(self.comp)

任何形式的帮助将不胜感激

您每次按下按钮时都会创建一个新的 QLabel,而您必须重新使用该 QLabel。

from PyQt4 import QtCore, QtGui


class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self.average_label = QtGui.QLabel()
        self.average_label.setFont(QtGui.QFont("sanSerif", 16))
        self.button = QtGui.QPushButton("Test")
        self.button.clicked.connect(self.on_clicked)

        lay = QtGui.QVBoxLayout(self)
        lay.addWidget(self.average_label)
        lay.addWidget(self.button)

    @QtCore.pyqtSlot()
    def on_clicked(self):
        import random
        self.average = random.uniform(0, 100) # emulate average
        self.average_label.setText(
            "The Students Average is {}".format(self.average)
        )


if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())