如何从PyQT5的菜单中调用新(弹出)窗口?

时间:2019-02-05 14:29:26

标签: python-3.x pyqt5

我是Python的初学者。如何从菜单调用弹出窗口(新)?我知道如何使用菜单。任何示例代码都值得赞赏。

这是菜单的示例代码。如何从菜单触发POP UP窗口操作?

import sys
import PyQt5.QtGui as Gui
import PyQt5.QtWidgets as Wid
import PyQt5.QtCore as Cor 

class winTable(Wid.QMainWindow):
def __init__(self, parent=None):
super(winTable, self).__init__(parent);
self.title = "Table";
self.top = 150;
self.left = 300;
self.width = 870;
self.height = 500;
self.setWindowTitle (self.title);
self.setGeometry (self.left, self.top, self.width, self.height); 

class mainWindow(Wid.QMainWindow):
def __init__(self, parent=None):
super(mainWindow, self).__init__(parent);
self.qtMenu();
def qtMenu(self):

mainMenu = self.menuBar();
pyGuiMenu = mainMenu.addMenu('File'); 

subItemTable = Wid.QAction('New', self);
subItemTable.setShortcut("Ctrl+N");
subItemTable.setStatusTip("New Window");
pyGuiMenu.addAction(subItemTable); 

subItemExit = Wid.QAction('Exit', self);
subItemExit.setShortcut("Ctrl+E");
subItemExit.setStatusTip("Exit Application");
subItemExit.triggered.connect(self.close_App);
pyGuiMenu.addAction(subItemExit);

def close_App(self):
reply = Wid.QMessageBox.question(self, "Exit Application", "Are you sure to close the window?", Wid.QMessageBox.Yes | Wid.QMessageBox.No, Wid.QMessageBox.No);
if reply == Wid.QMessageBox.Yes:
   sys.exit(); 

def main():
App = Wid.QApplication(sys.argv);
homeWin = mainWindow();   
homeWin.title = "Trial GUI";
homeWin.iconname = "pylogo.png";
homeWin.top = 250;
homeWin.left = 400;
homeWin.width = 870;
homeWin.height = 500;
homeWin.setWindowTitle (homeWin.title);
homeWin.setGeometry (homeWin.left, homeWin.top, homeWin.width, homeWin.height);
homeWin.setWindowIcon (Gui.QIcon(homeWin.iconname));
homeWin.show();
sys.exit(App.exec_()); 

if __name__ == "__main__":
main();`

如何从菜单触发“弹出窗口”操作?

1 个答案:

答案 0 :(得分:0)

尝试一下:

import sys
import PyQt5.QtGui     as Gui
import PyQt5.QtWidgets as Wid
import PyQt5.QtCore    as Cor 

class WinTable(Wid.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.title = "Table"
        self.top    = 150
        self.left   = 300
        self.width  = 870
        self.height = 500
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height) 

class mainWindow(Wid.QMainWindow):
    def __init__(self, parent=None):
        super(mainWindow, self).__init__(parent)

        self.qtMenu()

    def qtMenu(self):
        mainMenu = self.menuBar()
        pyGuiMenu = mainMenu.addMenu('File')

        subItemTable = Wid.QAction('New', self)
        subItemTable.setShortcut("Ctrl+N")
        subItemTable.setStatusTip("New Window")

        subItemTable.triggered.connect(self.newWindow)     # +++

        pyGuiMenu.addAction(subItemTable) 

        subItemExit = Wid.QAction('Exit', self)
        subItemExit.setShortcut("Ctrl+E")
        subItemExit.setStatusTip("Exit Application")
        subItemExit.triggered.connect(self.close_App)
        pyGuiMenu.addAction(subItemExit);

    def close_App(self):
        reply = Wid.QMessageBox.question(
            self, 
            "Exit Application", 
            "Are you sure to close the window?", 
            Wid.QMessageBox.Yes | Wid.QMessageBox.No, 
            Wid.QMessageBox.No
        )
        if reply == Wid.QMessageBox.Yes:
           sys.exit()

    def newWindow(self):                                    # +++
        print('def newWindow(self):')    
        self.winTable = WinTable()
        self.winTable.show()


def main():
    App = Wid.QApplication(sys.argv)
    homeWin = mainWindow()   
    homeWin.setWindowTitle("Trial GUI")
    homeWin.setGeometry (250, 200, 870, 500);
    homeWin.setWindowIcon (Gui.QIcon("D:/_Qt/img/pyqt.jpg"));
    homeWin.show();
    sys.exit(App.exec_()); 

if __name__ == "__main__":
    main()

enter image description here