Pyqt5 ошибка при запуске
Выдает ошибку:
File "main.py", line 10, in __init__
self.menubar = QMainWindow.menuBar()
TypeError: menuBar(self): first argument of unbound method must have type 'QMainWindow'
Как исправить ?
import sys
import PyQt5
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QMainWindow, QAction, qApp
from PyQt5.QtGui import QIcon
HEIGHT = 800
WIDTH = 1300
class Menu():
def __init__(self):
QMainWindow().__init__()
self.menubar = QMainWindow().menuBar()
self.file = self.menubar.addMenu('&File')
self.exitAction = QAction(QIcon('exit.png'), '&Exit', self)
self.exitAction.setShortcut('Ctrl+Q')
self.exitAction.setStatusTip('Exit application')
self.exitAction.triggered.connect(self.close)
self.file.addAction(self.exitAction)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
q = QDesktopWidget().availableGeometry()
r_w, r_h = q.width(), q.height()
self.setGeometry(r_w/2-WIDTH/2, r_h/2-HEIGHT/2, WIDTH, HEIGHT)
self.setFixedSize(1300, 800)
self.setWindowTitle('Lings')
self.setWindowIcon(QIcon('Lings.ico'))
menu = Menu()
self.show()
# class Window1(QMainWindow):
# def __init__(self):
# super().__init__()
# def initUI(self):
# mainWindow.menu()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
sys.exit(app.exec_())
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Как вариант:
import sys
import PyQt5
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QMainWindow, QAction, qApp
from PyQt5.QtGui import QIcon
HEIGHT = 800
WIDTH = 1300
class Menu(QWidget): # + QWidget
def __init__(self, parent=None): # + parent
super().__init__(parent) # + parent
# self.menubar = QMainWindow().menuBar()
self.menubar = parent.menuBar() # +++
self.file = self.menubar.addMenu('&File')
self.exitAction = QAction(QIcon('exit.png'), '&Exit', self)
self.exitAction.setShortcut('Ctrl+Q')
self.exitAction.setStatusTip('Exit application')
# self.exitAction.triggered.connect(self.close)
self.exitAction.triggered.connect(parent.close) # +++
self.file.addAction(self.exitAction)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# q = QDesktopWidget().availableGeometry()
# r_w, r_h = q.width(), q.height()
# print(r_w, r_h)
# self.setGeometry(r_w/2-WIDTH/2, r_h/2-HEIGHT/2, WIDTH, HEIGHT) # ???
# print(self.geometry())
# self.setFixedSize(1300, 800)
self.resize(WIDTH, HEIGHT) # +
self.setWindowTitle('Lings')
self.setWindowIcon(QIcon('Lings.ico'))
self.menu = Menu(self) # + self
# self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show() # +
sys.exit(app.exec_())
