QtabWidget 和 QMainWindow 合二为一 class

QtabWidget and QMainWindow in one class

有没有办法在 QtabWidget 中定义菜单栏 class?

我用 pyqt5 和 python 3.6 编写了一个 destopapplication 代码。我想将 manuBar 设置为与选项卡相同的 class 但我的代码 returns qtabwidget 没有属性 QMainWindow.

这是我的代码:

import sys
from PyQt5 import QtWidgets, QtCore, QtPrintSupport, QtGui
from PyQt5.QtWidgets import *

class main_window(QTabWidget):
    def __init__(self, parent=None):
        super(QTabWidget, self).__init__(parent)
        self.setGeometry(50, 50, 1100, 750)
        self.setWindowTitle("Programm")  #

        self.centralWidget = QtWidgets.QWidget()
        self.tabWidget = QtWidgets.QTabWidget(self.centralWidget)
        self.tabWidget.setGeometry(QtCore.QRect(10, 10, 1200, 1000))

        open_new_file = QAction('New', self)

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('Projekt')


        fileMenu.addAction(open_new_file)
        self.table_widget = MyTableWidget(self)
        self.setCentralWidget(self.table_widget)

        self.show()

        self.tab_v1 = QtWidgets.QWidget()
        self.addTab(self.tab_v1, "Tab 1")

        self.tab_v2 = QtWidgets.QWidget()
        self.addTab(self.tab_v2, "Tab 2")

        self.openFile = QPushButton("Choose Tab ", self.tab_v1)
        self.openFile.setGeometry(QtCore.QRect(700, 25, 200, 30))


def main():
    app = QApplication(sys.argv)
    ex = main_window()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

QTabWidget不能有QMenuBar,你要做的就是把一个QMainWindow的centralwidget放到QTabWidget上。

import sys
from PyQt5 import QtCore, QtWidgets

class Main_window(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Main_window, self).__init__(parent)
        self.setGeometry(50, 50, 1100, 750)
        self.setWindowTitle("Programm")  

        open_new_file = QtWidgets.QAction('New', self)
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('Projekt')
        fileMenu.addAction(open_new_file)

        self.tabWidget = QtWidgets.QTabWidget()
        self.setCentralWidget(self.tabWidget)

        self.tab_v1 = QtWidgets.QWidget()
        self.tabWidget.addTab(self.tab_v1, "Tab 1")
        self.openFile =QtWidgets.QPushButton("Choose Tab ", self.tab_v1)
        self.openFile.setGeometry(QtCore.QRect(700, 25, 200, 30))

        self.tab_v2 = QtWidgets.QWidget()
        self.tabWidget.addTab(self.tab_v2, "Tab 2")

def main():
    app = QtWidgets.QApplication(sys.argv)
    ex = Main_window()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()