带有可打开其他GUI的按钮的Master PyQt5 GUI

时间:2019-08-11 19:58:12

标签: python widget pyqt5

我想使用Python3和PyQt5,在GUI上带有一个按钮,按下该按钮将打开另一个具有按钮和其他控件的小部件。将来,主GUI将具有许多按钮,这些按钮将打开许多其他小部件。我正在寻找一个带有嵌入式matplotlib.pyplot的小部件。目前,我正在努力打开第二个小部件。

主程序

import sys

from PyQt5 import QtWidgets

from gui import Ui_MainWindow as Ui_MainWindow1
from gui2 import Ui_MainWindow as Ui_MainWindow2

class Window(QtWidgets.QMainWindow, Ui_MainWindow1):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.ui_gui = Ui_MainWindow1()
        self.ui_gui.setupUi(self)


        self.pb.clicked.connect(self.on_pb_clicked)
        self.graph = Graph(self)

    def on_pb_clicked(self):

        print('pb clicked')
        self.graph.show()


class Graph(QtWidgets.QMainWindow, Ui_MainWindow2):

    def __init__(self, parent=None):
        super(Graph, self).__init__(parent)

        self.ui_graph = Ui_MainWindow2()
        self.ui_graph.setupUi(self)


if __name__ == "__main__":
    #import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow1()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

gui = gui2。两者都如下图所示。

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(282, 219)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.pb = QtWidgets.QPushButton(self.centralWidget)
        self.pb.setGeometry(QtCore.QRect(100, 60, 75, 23))
        self.pb.setObjectName("pb")
        MainWindow.setCentralWidget(self.centralWidget)
        self.menuBar = QtWidgets.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 282, 21))
        self.menuBar.setObjectName("menuBar")
        MainWindow.setMenuBar(self.menuBar)
        self.mainToolBar = QtWidgets.QToolBar(MainWindow)
        self.mainToolBar.setObjectName("mainToolBar")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
        self.statusBar = QtWidgets.QStatusBar(MainWindow)
        self.statusBar.setObjectName("statusBar")
        MainWindow.setStatusBar(self.statusBar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pb.setText(_translate("MainWindow", "pb"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

我想单击gui上的按钮并显示gui2。 NB gui将来将不等于gui2。

2 个答案:

答案 0 :(得分:0)

由于WindowGraph分别从Ui_Window1Ui_Window2继承,因此您应该在self.setupUi(self)和{{1}中调用Window.__init__ }而不是分别创建Graph.__init__Ui_Window1的实例,即

Ui_Window2

程序的主要部分应类似于

class Window(QtWidgets.QMainWindow, Ui_MainWindow1):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.setupUi(self)
        self.pb.clicked.connect(self.on_pb_clicked)
        self.graph = Graph(self)

        # just to see the two windows side-by-side
        self.move(500, 400)
        self.graph.move(self.x()+self.width()+20, self.y())

    def on_pb_clicked(self):
        print('pb clicked')
        self.graph.show()


class Graph(QtWidgets.QMainWindow, Ui_MainWindow2):

    def __init__(self, parent=None):
        super(Graph, self).__init__(parent)
        self.setupUi(self)

答案 1 :(得分:0)

您使用QMetaObject::connectSlotsByName()启用on_pb_clicked()插槽的自动调用。

您不需要使用self.pb.clicked.connect(self.on_pb_clicked) 您需要@QtCore.pyqtSlot()

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from gui_1 import Ui_MainWindow as Ui_MainWindow1
from gui_2 import Ui_MainWindow as Ui_MainWindow2

class Graph(QtWidgets.QMainWindow, Ui_MainWindow2):
    def __init__(self, parent=None):
        super(Graph, self).__init__(parent)
        self.setupUi(self)

        self.setWindowTitle("window Graph")
        self.setGeometry(QtCore.QRect(850, 260, 282, 219))
#        self.pb.clicked.connect(self.on_pb_clicked)         # ---

    @QtCore.pyqtSlot()                                       # +++
    def on_pb_clicked(self):       
        print('pb clicked -> Graph')


class Window(QtWidgets.QMainWindow, Ui_MainWindow1):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.setWindowTitle("main Window")
#        self.pb.clicked.connect(self.on_pb_clicked)         # ---
        self.graph = Graph(self) 

    @QtCore.pyqtSlot()                                       # +++
    def on_pb_clicked(self): 
        print('pb clicked -> "main Window')
        self.graph.show()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

enter image description here