元素大小调整在子类中不起作用

时间:2019-05-10 11:21:38

标签: python pyqt

我正在尝试在主窗口的子类中调整按钮的大小,但是似乎没有任何效果。我应该使用其他功能吗?

我已经尝试过resize()函数,并且从其他搜索中也可以看到addStrech()。

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.form_widget = FormWidget(self)
        self.setCentralWidget(self.form_widget)
        self.setWindowTitle("Breast cancer predictor")
        # self.setFixedSize(500,500)


class FormWidget(QWidget):

    def __init__(self, parent):
        super(FormWidget, self).__init__(parent)
        self.file_path=None
        self.layout = QVBoxLayout(self)

        self.add_image_button = QPushButton('Add image for prediction', self)
        self.add_image_button.clicked.connect(self.openImage)
        self.add_image_button.resize(300, 32)
        self.layout.addWidget(self.add_image_button)

        self.label = QLabel()
        self.layout.addWidget(self.label)

        self.labelPrediction = QLabel()
        self.layout.addWidget(self.labelPrediction)

        self.make_prediction_button = QPushButton("Make prediction")
        self.make_prediction_button.clicked.connect(self.click_method)
        self.layout.addWidget(self.make_prediction_button)

        self.setLayout(self.layout)

    def openImage(self):

        imagePath, _ = QFileDialog.getOpenFileName()
        self.file_path=imagePath
        print(imagePath)
        pixmap = QPixmap(imagePath)

        self.label.setPixmap(pixmap)
        self.label.setAlignment(Qt.AlignCenter)
        self.resize(pixmap.size())
        self.adjustSize()

    def click_method(self):
        print("Din buton", self.file_path)

        subprocess.check_call('python ./UNetFirstTry.py ' + ' -i ' + self.file_path +' -o salcf.jpg --model ./checkpoints/CP5.pth', shell=True)
        imagePath = "E:/Mihaica/Faculta/An4/Licenta/untitled/salcf.jpg"

        pixmap = QPixmap(imagePath)
        self.labelPrediction.setPixmap(pixmap)
        self.labelPrediction.setAlignment(Qt.AlignCenter)
        self.resize(pixmap.size())
        self.adjustSize()

我希望按钮可以调整大小和修改,而不受窗口高度或宽度的影响。

0 个答案:

没有答案