当菜单类继承自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_())
尝试更换:
self.settings_btn = QPushButton()
与
self.settings_btn = QPushButton('PyQt5 button', self)