PyQt5,单击按钮后如何打开新窗口

Posted

技术标签:

【中文标题】PyQt5,单击按钮后如何打开新窗口【英文标题】:PyQt5, how to open new window after click QButton 【发布时间】:2019-01-10 06:46:17 【问题描述】:

我点击了按钮平台类型,但 Ui_Form 没有显示

我尝试了exec_(),但进程以exit code -1073740791 (0xC0000409) 结束

我想在点击按钮时打开新的 QWidget 窗口

详情:在 Python 中,当我按下第一个小部件的按钮时,我想再创建一个窗口。我试图关注其他内容,但程序以错误代码终止。如果您能告诉我问题所在,我将不胜感激。

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Main_Widget(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()
        self._mutex = QtCore.QThread()
        self.setupUi(Main_Widget)

    def setupUi(self, Main_Widget):
        Main_Widget.setObjectName('Main_Widget')
        Main_Widget.resize(1272, 640)

        self.All_GroupBox = QtWidgets.QGroupBox(Main_Widget)
        self.All_GroupBox.setGeometry(QtCore.QRect(20, 10, 1231, 611))
        font = QtGui.QFont()
        self.All_GroupBox.setFont(font)
        self.All_GroupBox.setObjectName('All_GroupBox')

        self.verticalLayoutWidget = QtWidgets.QWidget(self.All_GroupBox)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 30, 261, 331))
        self.verticalLayoutWidget.setObjectName('verticalLayoutWidget')

        self.Button_VerticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.Button_VerticalLayout.setContentsMargins(0, 0, 0, 0)
        self.Button_VerticalLayout.setObjectName('Button_VerticalLayout')

        self.PlatformType_Button = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.PlatformType_Button.setMinimumSize(QtCore.QSize(0, 45))
        self.PlatformType_Button.setObjectName('PlatformType_Button')
        self.Button_VerticalLayout.addWidget(self.PlatformType_Button)

    def PlatformType_Clicked(self):
        dialog = Ui_Form(self)
        self.dialogs.append(dialog)
        dialog.show()
        dialog.exec_()

class Ui_Form(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Ui_Form, self).__init__(parent)

    def setupUi(self, Form):
        Form.setObjectName('Form')
        Form.resize(422, 190)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    Main_Widget = QtWidgets.QWidget()
    ui = Ui_Main_Widget()
    Main_Widget.show()
    app.exec_()

【问题讨论】:

【参考方案1】:

试试看:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Main_Widget(QtWidgets.QWidget):

#    def __init__(self):
#        super().__init__()
#        self._mutex = QtCore.QThread()
#        self.setupUi(Main_Widget)

    def setupUi(self, Main_Widget):
        Main_Widget.setObjectName('Main_Widget')
        Main_Widget.resize(1272, 640)

        self.All_GroupBox = QtWidgets.QGroupBox(Main_Widget)
        self.All_GroupBox.setGeometry(QtCore.QRect(20, 10, 1231, 611))
        font = QtGui.QFont()
        self.All_GroupBox.setFont(font)
        self.All_GroupBox.setObjectName('All_GroupBox')

        self.verticalLayoutWidget = QtWidgets.QWidget(self.All_GroupBox)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 30, 261, 331))
        self.verticalLayoutWidget.setObjectName('verticalLayoutWidget')

        self.Button_VerticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.Button_VerticalLayout.setContentsMargins(0, 0, 0, 0)
        self.Button_VerticalLayout.setObjectName('Button_VerticalLayout')

        self.PlatformType_Button = QtWidgets.QPushButton("PlatformType_Button", self.verticalLayoutWidget)
        self.PlatformType_Button.setMinimumSize(QtCore.QSize(0, 45))
        self.PlatformType_Button.setObjectName('PlatformType_Button')
# +++        
        self.PlatformType_Button.clicked.connect(Main_Widget.PlatformType_Clicked) # +++

        self.Button_VerticalLayout.addWidget(self.PlatformType_Button)

#    def PlatformType_Clicked(self):
#        dialog = Ui_Form(self)
#        self.dialogs.append(dialog)
#        dialog.show()
#        dialog.exec_()

class Ui_Form(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Ui_Form, self).__init__(parent)

    def setupUi(self, Form):
        Form.setObjectName('Form')
        Form.resize(422, 190)


class MyWindow(QtWidgets.QWidget):                     # +++
    def __init__(self):
        super().__init__()
        self.ui = Ui_Main_Widget() 
        self.ui.setupUi(self)

# +++
    def PlatformType_Clicked(self):
        self.dialog = Ui_Form()            # --- self
#        self.dialogs.append(dialog)
        self.dialog.show()
#        self.dialog.exec_()


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

【讨论】:

【参考方案2】:

hide() 和 show() 方法你可以随意使用多个对话框,...

def PlatformType_Clicked(self): dialog.hide() dialog1.show()

【讨论】:

谢谢您,先生。但在我的代码中,show 或 exec_ 不起作用,并且窗口关闭,只是以退出代码结束。

以上是关于PyQt5,单击按钮后如何打开新窗口的主要内容,如果未能解决你的问题,请参考以下文章

单击python kivy中的按钮后如何打开弹出窗口?

如何在pytest-qt中单击按钮后检查正确打开窗口

在 PyQt5 中打开一个窗口和关闭一个窗口

我想用Swing实现单击按钮后能弹出一个新窗口!!这是我的代码!谢谢!

单击按钮后使用 Selenium 切换到新窗口

如何单击按钮并打开窗口以在c#中发送电子邮件