如何在其他窗口中创建菜单栏

时间:2019-05-25 12:32:57

标签: python-3.x pyqt5

我有2个窗口。主窗口“窗口”具有菜单栏和按钮,可打开窗口MainDB。我无法在MainDB上添加菜单栏。当我从Window在MainDB按钮上添加菜单栏时,关闭我的应用程序。 MainDB窗口也可以具有按钮,这些按钮可以打开新表单。

import sys
from pprint import pprint
from PyQt5 import QtGui
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication, QPushButton, QLabel,QDialog
from PyQt5.QtGui import QIcon,QPixmap
from PyQt5.QtCore import QRect, pyqtSlot


class Window (QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Choice suppliers ")
        self.setWindowIcon(QtGui.QIcon('icon.png'))
        self.setGeometry(600, 300, 500, 350)
        self.buttons()
        self.bar()

    def bar(self):
        # --------bar---------
        menuBar = self.menuBar()
        filemenu = menuBar.addMenu('File')
        aboutmenu = menuBar.addMenu('About')
        exit_action = QAction( 'Exit', self)
        exit_action.setShortcut('Ctrl+Q')
        exit_action.setStatusTip('Exit program')
        exit_action.triggered.connect(qApp.quit)
        filemenu.addAction(exit_action)

    def buttons(self):
        but_db = QPushButton("Add New date", self)
        but_db.clicked.connect(self.but_db_click)
        but_ord = QPushButton("Order", self)         
        but_db.setGeometry(QRect(60, 130, 160, 70))
        but_ord.setGeometry(QRect(270, 130, 160, 70)) 

    @pyqtSlot()
    def but_db_click(self):
        self.cams = MainDB()
        self.cams.show()
        self.close()

class MainDB (QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Window2")
        self.setWindowIcon(QtGui.QIcon('icon.png'))
        self.setGeometry(600, 300, 500, 350)

if __name__ == '__main__':
    app = QApplication(sys.argv)

    app.setStyle('Fusion')
    # --------Style---------
    app.setStyleSheet(
        "QPushButton {"
        "font-family: Lucida Console,serif;"
        "font-size:14px}"
        "QMainWindow{background-color:white;}")
    Window = Window()
    Window.show()
    sys.exit(app.exec_())

0 个答案:

没有答案