Python(QtDesigner)使用按钮

时间:2017-06-22 15:55:27

标签: python python-3.x pyqt pyqt5 qt-designer

我使用QtDesigner来创建我的所有窗口,主窗口使用mdiArea,以便在调用时其他窗口适合它。我的主要问题是当我关闭mdiArea中的一个窗口时它会消失并且我无法重新打开它,id或者像没有退出按钮的窗口一样,或者创建一个带有按钮的窗口,如果不存在则打开文件。 / p>

输出截图

最小化的窗口是ValveSimulator,如果我关闭它,它就不再存在了,我无法打开它

我的主要代码:CreateWindow使用按钮创建窗口,CreateValveSimulator是我的主程序,我想在关闭它时从按钮打开

from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget,QWidget, QMdiSubWindow
import ValveSim
import MainWindow
import Window

class Win1(QMainWindow):
    def __init__(self):
        QWidget.__init__(self)
        self.vtn = MainWindow.Ui_MainWindow()
        self.vtn.setupUi(self)
        self.subwindow = QMdiSubWindow()
        self.CreateValveSimulator()
        self.CreateWindow()

    def CreateValveSimulator(self):
        widget = QMainWindow()
        self.VSim_subwindow = ValveSim.Ui_ValveSim()
        self.VSim_subwindow.setupUi(widget)
        self.subwindow = QMdiSubWindow(self.vtn.mdiArea)
        widget.setParent(self.subwindow)
        self.subwindow.setWidget(widget)
        self.vtn.mdiArea.addSubWindow(self.subwindow)
        widget.show()
        self.subwindow.show()
        self.subwindow.widget().show()

    def CreateWindow(self):
        widget = QMainWindow()
        self.win_subwindow = Window.Ui_MainWindow()
        self.win_subwindow.setupUi(widget)
        self.subwindow = QMdiSubWindow(self.vtn.mdiArea)
        widget.setParent(self.subwindow)
        self.subwindow.setWidget(widget)
        self.vtn.mdiArea.addSubWindow(self.subwindow)
        widget.show()
        self.subwindow.show()
        self.subwindow.widget().show()

if __name__=='__main__':
    app =QApplication(sys.argv)
    win = Win1()
    win.show()
    sys.exit(app.exec_())

我将self.pushbutton连接到一个名为openvs的函数,我希望在它关闭时打开Valve Simulator窗口,如我的主代码所示,但是当我点击按钮时它就崩溃了

带按钮代码的表:

 from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QWidget, QMdiSubWindow, QAction
import Untitled
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setWindowModality(QtCore.Qt.WindowModal)
        MainWindow.resize(351, 442)
        MainWindow.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(85, 125, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(85, 195, 75, 23))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(200, 125, 75, 23))
        self.pushButton_3.setObjectName("pushButton_3")
        MainWindow.setCentralWidget(self.centralwidget)
        self.pushButton.clicked.connect(self.openvs)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Open Valve Simulator"))
        self.pushButton_2.setText(_translate("MainWindow", "Open Module Program"))
        self.pushButton_3.setText(_translate("MainWindow", "Open Metal Sizing"))

    def openvs(self):
        Untitled.Win1.CreateValveSimulator()

1 个答案:

答案 0 :(得分:0)

您必须单独创建窗口,并将它们添加到每个QMdiSubWindow。当窗口小部件显示在子窗口中时,这将导致显示子窗口。要使用按钮正常打开,必须使用单击的信号并调用showNormal函数。

完整代码为here

class HomeWindow(QMainWindow, Ui_HomeWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent=parent)
        self.setupUi(self)


class ValveSim(QMainWindow, Ui_ValveSim):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent=parent)
        self.setupUi(self)


class Win1(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent=parent)
        self.setupUi(self)
        self.vs = ValveSim()
        self.hw = HomeWindow()
        self.CreateValveSimulator()
        self.CreateWindow()

    def CreateValveSimulator(self):
        subwindow = QMdiSubWindow()
        subwindow.setWidget(self.vs)
        self.mdiArea.addSubWindow(subwindow)
        subwindow.setFixedSize(500, 500)
        # self.subwindow.close()

    def CreateWindow(self):
        self.hw.pushButton.clicked.connect(self.vs.showNormal)
        subwindow = QMdiSubWindow()
        subwindow.setWindowFlags(Qt.CustomizeWindowHint | Qt.Tool)
        subwindow.setWidget(self.hw)
        self.mdiArea.addSubWindow(subwindow)