是否可以在QtabWidget类中定义菜单栏?
我用pyqt5和python 3.6编写了一个destopapplication的代码。我想在与Tabs相同的类中设置manuBar,但是我的代码返回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()
答案 0 :(得分:1)
QTabWidget不能具有QMenuBar,您要做的就是将QMainWindow的中央部件放到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()