为什么PyQt5不显示按钮?

问题描述 投票:0回答:1

当菜单类继承自QWidgets类时,会显示该按钮,但是,当继承自QMainWindow时,不会显示相同的按钮。我到处搜索但找不到解释。

from PyQt5.QtWidgets import QMainWindow, QFormLayout, QPushButton, QApplication, QWidget
import sys


class Menu(QMainWindow):
    """
    Create menu object
    """

    def __init__(self, parent=None):
        """
        Initialize main menu
        """
        super().__init__(parent)
        self.file = None
        self.fields_create()
        self.create_table()

    def fields_create(self):
        """
        Create buttons and LineEdits
        """
        # Settings Button
        self.settings_btn = QPushButton()
        self.settings_btn.setText("Settings")
        self.settings_btn.clicked.connect(self.settings_click)

    def create_table(self):
        """
        create and display table
        """
        self.flo = QFormLayout()
        self.flo.addRow(self.settings_btn)
        self.setLayout(self.flo)

    def settings_click(self):
        print("clicked")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Menu()
    win.show()
    sys.exit(app.exec_())

python pyqt
1个回答
0
投票

尝试更换:

self.settings_btn = QPushButton()

self.settings_btn = QPushButton('PyQt5 button', self)
© www.soinside.com 2019 - 2024. All rights reserved.