单击主窗口PyQt5 Python中的按钮时打开窗口

单击主窗口PyQt5 Python中的按钮时打开窗口,python,user-interface,pyqt,pyqt5,Python,User Interface,Pyqt,Pyqt5,我已经通过了各种类似的帖子,这些帖子都有类似的问题,我也尝试了答案,但对我来说不起作用。这就是我的问题- 我有两个窗口-TestBox和MailBox,当我单击TestBox按钮和输入路径参数时,它必须打开邮箱窗口,当我关闭邮箱窗口时,这个新窗口邮箱将再次向TestBox发送修改路径 我在PyQt5工具中创建了两个窗口,并创建了ui文件,然后使用-Python-m PyQt5.uic.pyuic testboxui.ui-o testboxui.py转换为Python文件 我没有接触testbo

我已经通过了各种类似的帖子,这些帖子都有类似的问题,我也尝试了答案,但对我来说不起作用。这就是我的问题-

我有两个窗口-TestBox和MailBox,当我单击TestBox按钮和输入路径参数时,它必须打开邮箱窗口,当我关闭邮箱窗口时,这个新窗口邮箱将再次向TestBox发送修改路径

  • 我在PyQt5工具中创建了两个窗口,并创建了ui文件,然后使用-Python-m PyQt5.uic.pyuic testboxui.ui-o testboxui.py转换为Python文件

  • 我没有接触testboxui.py或mailboxui.py文件,因为它们通过重新转换不断更改任何修改。相反,我创建了另一个文件TestBox.py和MailBox.py来导入和写入函数

  • 这是可生产的最小代码-

    TestBox.py[主应用程序代码]

    import os,sys
    from PyQt5 import QtWidgets, QtGui, QtCore
    from testboxui import Ui_TestBox
    from MailBox import AppWindow_MailList
    
    app = QtWidgets.QApplication(sys.argv)
    
    class AppWindow_MainBox(QtWidgets.QMainWindow):
        def __init__(self):
            super(AppWindow_MainBox, self).__init__()
            self.ui = Ui_TestBox()
            self.ui.setupUi(self)
            self.ui.openbox.clicked.connect(self.fcn_maillist)
    
        def fcn_maillist(self):
            GetPath = os.getcwd()
            dialog  = AppWindow_MailList(self,GetPath)
            if dialog.exec_() == AppWindow_MailList.Accepted:
                GetPath = dialog.get_output()
    
    def main():
        app = QtWidgets.QApplication(sys.argv)
        application = AppWindow_MainBox()
        application.show()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    
    testboxui.py[由PyQt5工具直接从UI文件生成]

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_TestBox(object):
        def setupUi(self, TestBox):
            TestBox.setObjectName("TestBox")
            TestBox.resize(647, 279)
            self.centralwidget = QtWidgets.QWidget(TestBox)
            self.centralwidget.setObjectName("centralwidget")
            self.openbox = QtWidgets.QPushButton(self.centralwidget)
            self.openbox.setGeometry(QtCore.QRect(210, 60, 231, 91))
            self.openbox.setObjectName("openbox")
            TestBox.setCentralWidget(self.centralwidget)
    
            self.retranslateUi(TestBox)
            QtCore.QMetaObject.connectSlotsByName(TestBox)
    
        def retranslateUi(self, TestBox):
            _translate = QtCore.QCoreApplication.translate
            TestBox.setWindowTitle(_translate("TestBox", "TestBox"))
            self.openbox.setText(_translate("TestBox", "Click Here \n""To Open Another Window"))
    
    MailBox.py[子应用程序代码]

    import os,sys
    from PyQt5 import QtWidgets, QtGui, QtCore
    from maillistui import Ui_MailList
    
    app = QtWidgets.QApplication(sys.argv)
    
    class AppWindow_MailList(QtWidgets.QMainWindow):
        def __init__(self,RcvPath=''):
            super(AppWindow_MailList, self).__init__()
            self.ui = Ui_MailList()
            self.ui.setupUi(self)
            self.init_fcn(RcvPath)
    
        def init_fcn(self,RcvPath):
            self.ui.browse.clicked.connect(self.browse_fcn) 
            if not RcvPath:
                self.RcvPath = os.getcwd()
            else:
                self.RcvPath = RcvPath
            self.ui.path.setText(self.RcvPath)
    
        def closeEvent(self, event):
            event.accept()
    
        def get_output(self):
            return os.path.join(self.RcvPath,'TestFolder')
    
        def browse_fcn(self):
            pass
    
    def main():
        app = QtWidgets.QApplication(sys.argv)
        application = AppWindow_MailList()
        application.show()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    
    maillistui.py-[由PyQt5直接从ui文件生成]

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_MailList(object):
        def setupUi(self, MailList):
            MailList.setObjectName("MailList")
            MailList.resize(647, 279)
            self.centralwidget = QtWidgets.QWidget(MailList)
            self.centralwidget.setObjectName("centralwidget")
            self.path = QtWidgets.QLineEdit(self.centralwidget)
            self.path.setGeometry(QtCore.QRect(50, 30, 511, 41))
            self.path.setObjectName("path")
            self.browse = QtWidgets.QPushButton(self.centralwidget)
            self.browse.setGeometry(QtCore.QRect(450, 90, 112, 41))
            self.browse.setObjectName("browse")
            MailList.setCentralWidget(self.centralwidget)
    
            self.retranslateUi(MailList)
            QtCore.QMetaObject.connectSlotsByName(MailList)
    
        def retranslateUi(self, MailList):
            _translate = QtCore.QCoreApplication.translate
            MailList.setWindowTitle(_translate("MailList", "MailList"))
            self.browse.setText(_translate("MailList", "Browse"))
    
    以前我使用qtwidts.QDialog创建PyQt5对话框,我可以在qtwidts.QApplication中轻松加载该对话框。但这里两者都是QApplication(QMainWindow),但很难在ui中调用ui


    我在这里做错什么了吗

    只应创建一个QApplication,对于不必要创建的每个脚本,请删除以下代码:

    ```
    from MailBox import AppWindow_MailList
    
    app = QtWidgets.QApplication(sys.argv) # <--- delete this line
    
    class AppWindow_MainBox(QtWidgets.QMainWindow):
    ```
    
    ```
    from maillistui import Ui_MailList
    
    app = QtWidgets.QApplication(sys.argv) # <--- delete this line
    
    class AppWindow_MailList(QtWidgets.QMainWindow):
    ```
    
    这清楚地表明AppWindow\u邮件列表类接受单个参数(
    RcvPath
    ),但您将其传递给2。我不知道这是否是一个打字错误,或者您不知道“self”在python中的用法(如果是后者,建议阅读)

    即使解决了这个错误,还有另一个问题,AppWindow\u MailList是一个QMainWindow,因此它没有任何exec\u()方法,似乎您试图使用一些使用QDialog的帖子的代码,但不理解解决方案的逻辑

    通常,每个小部件都有一个用途:

    • QWidget是一个通用的小部件,可以用作构建任何其他类型的小部件或容器的基础,类似于html中的div

    • QDialog是一个小部件,它的目的是向用户请求信息,因此exec_()方法返回阻塞eventloop的请求的状态(如果它被接受或拒绝)

    • QMainWindow的目的是提供一个主窗口,因为它包含工具栏、状态栏、菜单栏、dockwidets等

    因此,为了实现您的目标,您必须选择正确的元素,考虑到我将重新构造您的应用程序

    mailbox.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>Dialog</class>
     <widget class="QDialog" name="Dialog">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>461</width>
        <height>300</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Dialog</string>
      </property>
      <layout class="QGridLayout" name="gridLayout">
       <item row="0" column="0" colspan="2">
        <widget class="QLineEdit" name="mail_le">
         <property name="minimumSize">
          <size>
           <width>0</width>
           <height>49</height>
          </size>
         </property>
         <property name="maximumSize">
          <size>
           <width>16777215</width>
           <height>40</height>
          </size>
         </property>
        </widget>
       </item>
       <item row="1" column="0">
        <spacer name="horizontalSpacer">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>324</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="1" column="1">
        <widget class="QPushButton" name="browse_btn">
         <property name="minimumSize">
          <size>
           <width>110</width>
           <height>40</height>
          </size>
         </property>
         <property name="maximumSize">
          <size>
           <width>16777215</width>
           <height>110</height>
          </size>
         </property>
         <property name="text">
          <string>Browse</string>
         </property>
        </widget>
       </item>
       <item row="2" column="1">
        <spacer name="verticalSpacer">
         <property name="orientation">
          <enum>Qt::Vertical</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>20</width>
           <height>178</height>
          </size>
         </property>
        </spacer>
       </item>
      </layout>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    邮箱.py

    import os
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    from mailbox_ui import Ui_Dialog
    
    
    class MailBox(QtWidgets.QDialog):
        def __init__(self, mail="", parent=None):
            super().__init__(parent)
    
            self.ui = Ui_Dialog()
            self.ui.setupUi(self)
    
            self.ui.browse_btn.clicked.connect(self.accept)
    
            if mail:
                self.ui.mail_le.setText(mail)
            else:
                self.ui.mail_le.setText(os.getcwd())
    
        def mail(self):
            return self.ui.mail_le.text()
    
    
    def main():
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = MailBox()
        w.show()
        sys.exit(app.exec_())
    
    
    if __name__ == "__main__":
        main()
    
    testbox.py

    import os
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    from mailbox import MailBox
    from testbox_ui import Ui_MainWindow
    
    
    class TestBox(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            self.ui.open_btn.clicked.connect(self.open_window)
    
        @QtCore.pyqtSlot()
        def open_window(self):
            dialog = MailBox()
            if dialog.exec_() == QtWidgets.QDialog.Accepted:
                path = dialog.mail()
                print(path)
    
    
    def main():
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = TestBox()
        w.show()
        sys.exit(app.exec_())
    
    
    if __name__ == "__main__":
        main()
    

    提供一个@eyllanesc-我尽了最大努力与下面的示例分享最小的可生产问题,但是您将代码设置得非常小(不必要------>修剪minimum@eyllanesc-抱歉延迟发布。以上评论是为了在下面发布。我知道我的第一个邮政编码不是reproducible@eyllanesc-我有几个应用程序已将QDialog导入QMainWindow应用程序,这些应用程序运行正常。我的问题是,这两个代码都是由PyQt5生成的用户界面文件和使用QMainWindow无法从其他QMainWindow应用程序打开。正如您正确指出的,我对两个窗口都使用QApplication,这不是正确的方法。在某些应用程序中,我通过手动编写代码(不是通过PyQt5工具)使用QDialog并且能够导入到MainWindow。由于我在这里使用PyQt5工具,所以我遇到了问题。现在我在UI文件中将QMainWindow更改为QDialog并再次转换。现在它工作得很好。感谢您在使用解决我问题的QTWidget时所作的说明。
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <layout class="QGridLayout" name="gridLayout">
        <item row="0" column="1">
         <spacer name="verticalSpacer">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>20</width>
            <height>213</height>
           </size>
          </property>
         </spacer>
        </item>
        <item row="1" column="0">
         <spacer name="horizontalSpacer">
          <property name="orientation">
           <enum>Qt::Horizontal</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>267</width>
            <height>20</height>
           </size>
          </property>
         </spacer>
        </item>
        <item row="1" column="1">
         <widget class="QPushButton" name="open_btn">
          <property name="minimumSize">
           <size>
            <width>230</width>
            <height>90</height>
           </size>
          </property>
          <property name="maximumSize">
           <size>
            <width>230</width>
            <height>90</height>
           </size>
          </property>
          <property name="text">
           <string>Click Here 
    To Open Another Window</string>
          </property>
         </widget>
        </item>
        <item row="1" column="2">
         <spacer name="horizontalSpacer_2">
          <property name="orientation">
           <enum>Qt::Horizontal</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>267</width>
            <height>20</height>
           </size>
          </property>
         </spacer>
        </item>
        <item row="2" column="1">
         <spacer name="verticalSpacer_2">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>20</width>
            <height>212</height>
           </size>
          </property>
         </spacer>
        </item>
       </layout>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>800</width>
         <height>26</height>
        </rect>
       </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    python -m PyQt5.uic.pyuic mailbox.ui -o mailbox_ui.py -x 
    python -m PyQt5.uic.pyuic testbox.ui -o testbox_ui.py -x 
    
    import os
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    from mailbox_ui import Ui_Dialog
    
    
    class MailBox(QtWidgets.QDialog):
        def __init__(self, mail="", parent=None):
            super().__init__(parent)
    
            self.ui = Ui_Dialog()
            self.ui.setupUi(self)
    
            self.ui.browse_btn.clicked.connect(self.accept)
    
            if mail:
                self.ui.mail_le.setText(mail)
            else:
                self.ui.mail_le.setText(os.getcwd())
    
        def mail(self):
            return self.ui.mail_le.text()
    
    
    def main():
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = MailBox()
        w.show()
        sys.exit(app.exec_())
    
    
    if __name__ == "__main__":
        main()
    
    import os
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    from mailbox import MailBox
    from testbox_ui import Ui_MainWindow
    
    
    class TestBox(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            self.ui.open_btn.clicked.connect(self.open_window)
    
        @QtCore.pyqtSlot()
        def open_window(self):
            dialog = MailBox()
            if dialog.exec_() == QtWidgets.QDialog.Accepted:
                path = dialog.mail()
                print(path)
    
    
    def main():
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = TestBox()
        w.show()
        sys.exit(app.exec_())
    
    
    if __name__ == "__main__":
        main()
    
    ├── mailbox.py
    ├── mailbox.ui
    ├── mailbox_ui.py
    ├── testbox.py
    ├── testbox.ui
    └── testbox_ui.py