Не отображается текст при нажатии на кнопку

Написал программу для расчета площади. Ошибок нет, но когда нажимаю рассчитать площадь, то в в лэйбле рядом с кнопкой результат не отображается.

Что я сделал не так?

import sys
from PyQt6.QtWidgets import *
import math

class Pl(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('MMM')
        self.resize(500, 500)

        self.Shape_cb = QComboBox(self)
        self.lbl = QLabel('Height', self)
        self.lbl2 = QLabel('Width', self)
        self.sq_heigh_spx = QSpinBox(self)
        self.sq_heigh_spx.setValue(10)
        self.sq_heigh_spx.setMinimum(10)
        self.sq_heigh_spx.setMaximum(9999)
        self.sq_width_spx = QSpinBox(self)
        self.sq_width_spx.setValue(10)
        self.sq_width_spx.setMinimum(10)
        self.sq_width_spx.setMaximum(9999)
        self.lbl3 = QLabel('Radius', self)
        self.cr_radius_spx = QSpinBox(self)
        self.cr_radius_spx.setValue(10)
        self.cr_radius_spx.setMinimum(10)
        self.cr_radius_spx.setMaximum(9999)
        self.btn = QPushButton('Compute', self)
        self.lbl4_result = QLabel('Result            ', self)

        self.group = QGroupBox('select shape', self)
        self.group.resize(490, 100)
        self.group.move(5, 0)

        self.group2 = QVBoxLayout(self)
        self.group2.addWidget(self.Shape_cb)
        self.group.setLayout(self.group2)

        self.sq_gb = QGroupBox('Square', self)
        self.sq_gb.move(5, 120)
        self.sq_gb.resize(490, 100)
        self.sq_gb.setAlignment(4)

        self.Shape_cb.addItem('Square')
        self.Shape_cb.addItem('Circle')

        self.gr = QFormLayout(self)
        self.sq_gb.setLayout(self.gr)
        self.gr.addRow(self.lbl, self.sq_heigh_spx)
        self.gr.addRow(self.lbl2, self.sq_width_spx)
        self.gr.setHorizontalSpacing(50)

        self.cr_gb = QGroupBox('Circle', self)
        self.cr_gb.move(5, 250)
        self.cr_gb.resize(490, 100)
        self.cr_gb.setAlignment(4)

        self.gr2 = QFormLayout(self)
        self.cr_gb.setLayout(self.gr2)
        self.gr2.addRow(self.lbl3, self.cr_radius_spx)

        self.btn.move(200, 400)

        self.lbl4_result.move(50, 450)

        self.btn.clicked.connect(self.compute)

        # Connects
        self.Shape_cb.currentIndexChanged.connect(self.update_UI)

        # Start
        self.update_UI()

    def update_UI(self):
        self.sq_gb.setVisible(self.Shape_cb.currentIndex() == 0)
        self.cr_gb.setVisible(self.Shape_cb.currentIndex() == 1)

    def compute(self):
        if self.Shape_cb.currentIndex == 0:
            self.computeSquare()
        elif self.Shape_cb.currentIndex == 1:
            self.computeCircle()

    def computeSquare(self):
        w = self.sq_width_spx.value()
        h = self.sq_heigh_spx.value()
        area = w * h
        self.showResult(area)

    def computeCircle(self):
        r = self.cr_radius_spx.value()
        area = math.pi * (r ** 2)
        self.showResult(area)

    def showResult(self, result):
        self.lbl4_result.setText('Результат: %s' % result)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    pl = Pl()
    pl.show()
    sys.exit(app.exec())

введите сюда описание изображения


Ответы (1 шт):

Автор решения: S. Nick

Я отметил для вас места, в которые внес изменения.

import sys
import math
'''
from PyQt6.QtWidgets import *
'''
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *


class Pl(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('MMM')
        self.resize(500, 500)

# ---------->v
        self.shape_cb = QComboBox(self)
        self.shape_cb.addItems(['Square', 'Circle'])
        self.shape_cb.currentIndexChanged.connect(self.update_UI)
        self.shape_cb.setCurrentIndex(0)
        
        self.lbl = QLabel('Height', self)
        self.lbl2 = QLabel('Width', self)
        
        self.sq_heigh_spx = QSpinBox(self)
        self.sq_heigh_spx.setValue(10)
        self.sq_heigh_spx.setMinimum(10)
        self.sq_heigh_spx.setMaximum(9999)
        self.sq_width_spx = QSpinBox(self)
        self.sq_width_spx.setValue(10)
        self.sq_width_spx.setMinimum(10)
        self.sq_width_spx.setMaximum(9999)
        self.lbl3 = QLabel('Radius', self)
        self.cr_radius_spx = QSpinBox(self)
        self.cr_radius_spx.setValue(10)
        self.cr_radius_spx.setMinimum(10)
        self.cr_radius_spx.setMaximum(9999)
        
        self.btn = QPushButton('Compute', self)
        self.btn.move(200, 400)
        self.btn.clicked.connect(self.compute)
        
        self.lbl4_result = QLabel('Result            ', self)
        self.lbl4_result.move(50, 450)

        self.group = QGroupBox('select shape', self)
        self.group.resize(490, 100)
        self.group.move(5, 0)

        self.group2 = QVBoxLayout(self)
        self.group2.addWidget(self.shape_cb)
        self.group.setLayout(self.group2)

        self.sq_gb = QGroupBox('Square', self)
        self.sq_gb.move(5, 120)
        self.sq_gb.resize(490, 100)
        self.sq_gb.setAlignment(4)

        self.gr = QFormLayout(self)
        self.sq_gb.setLayout(self.gr)
        self.gr.addRow(self.lbl, self.sq_heigh_spx)
        self.gr.addRow(self.lbl2, self.sq_width_spx)
        self.gr.setHorizontalSpacing(50)

        self.cr_gb = QGroupBox('Circle', self)
        self.cr_gb.move(5, 250)
        self.cr_gb.resize(490, 100)
        self.cr_gb.setAlignment(4)
        self.cr_gb.setVisible(False)                      # +++


        self.gr2 = QFormLayout(self)
        self.cr_gb.setLayout(self.gr2)
        self.gr2.addRow(self.lbl3, self.cr_radius_spx)

# --------------------->vvvvv
    def update_UI(self, index):
        self.sq_gb.setVisible(not index)
        self.cr_gb.setVisible(index)

    def compute(self):
# ---------------------------------->VV  
        if self.shape_cb.currentIndex() == 0:                   # !!! 
            self.computeSquare()
# ------------------------------------>VV                       # !!! 
        elif self.shape_cb.currentIndex() == 1:
            self.computeCircle()

    def computeSquare(self):
        w = self.sq_width_spx.value()
        h = self.sq_heigh_spx.value()
        area = w * h
        self.showResult(area)

    def computeCircle(self):
        r = self.cr_radius_spx.value()
        area = math.pi * (r ** 2)
        self.showResult(area)

    def showResult(self, result):
#        self.lbl4_result.setText('Результат: %s' % result)
        self.lbl4_result.setText(f'Результат: {result:.2f}')
        
# Регулирует размер виджета в соответствии с его содержимым.
        self.lbl4_result.adjustSize()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    pl = Pl()
    pl.show()
    sys.exit(app.exec())

введите сюда описание изображения

введите сюда описание изображения

→ Ссылка