python pyq4小部件在执行文件后退出

时间:2017-01-13 11:51:47

标签: python user-interface pyqt4

我有一个python项目。我是pyqt4的新手,但我遇到了一个问题,无法找到解决方案。我的项目有一个名为main.py的主文件,其内容如下:

import os
import sys

from PyQt4 import QtGui, uic
import setup, modules


class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        super(MyWindow, self).__init__()
        file_path = os.path.abspath("form_ui/main_window.ui")
        uic.loadUi(file_path, self)
        self.show()
        self.statusBar().showMessage('Developer contact: admin.unu@protonmail.com')                #message on status bar of main window
        self.actionExit.triggered.connect(self.close)                                              #action to close main windows
        self.actionSetup.triggered.connect(setup.setup_gui.setupgui)                               #action to run setup_guy.py
        self.actionUser.triggered.connect(modules.user_form.user_set)                              #action to run user_form.py
        self.actionClient_check.triggered.connect(modules.client_read.client_read_op)              #action to rin client_read.py
        self.actionClient_add.triggered.connect(modules.client_make.client_make_op)                #action to run client_make.py
        self.actionEquipment_add.triggered.connect(modules.add_equipment.add_equipment_op)         #action to run add_equipment.py
        self.actionOrders.triggered.connect(modules.comanda.comanda_def)                           #action to run comanda.py
        self.actionServices.triggered.connect(modules.services.add_services_op)                    #action to run services.py
        self.actionList_data.triggered.connect(modules.list_data.list_data_op)                     #action to run list_data.py
        self.move(QtGui.QApplication.desktop().screen().rect().center()- self.rect().center())     #center main windows

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

应用程序正在启动,gui启动。每个动作都会启动一个qwidget。这是一个qwidget示例:

import sys,os.path
from PyQt4 import QtGui, uic

class user_set(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        super(user_set, self).__init__()
        file_path = os.path.abspath("form_ui/user_form.ui")
        uic.loadUi(file_path, self)
        self.show()
        self.setFixedSize(self.size())
        self.move(QtGui.QApplication.desktop().screen().rect().center() - self.rect().center())
        sys.exit(app)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = user_set()
    window.show()
    sys.exit(app.exec_())

当主窗口尝试运行窗口小部件时,它立即关闭(窗口小部件不是主窗口)。我已插入行sys.exit(app)并且小部件运行正常,但在pycharm中我收到此错误sys.exit(app) NameError: global name 'app' is not defined。如果我尝试只运行窗口小部件文件,则会出现窗口并立即退出,我必须对de sys.exit(app)行进行评论。

有人有任何想法,我做错了吗?谢谢

EDIT1 ---------------------------------------------- ---------------------

他是我的主要档案:

import os
import sys

from PyQt4 import QtGui, uic
import setup, modules


class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        super(MyWindow, self).__init__()
        file_path = os.path.abspath("form_ui/main_window.ui")
        uic.loadUi(file_path, self)
        self.show()
        self.statusBar().showMessage('Developer contact: admin.unu@protonmail.com')                #message on status bar of main window
        self.actionExit.triggered.connect(self.close)                                              #action to close main windows
        self.actionSetup.triggered.connect(self.run_dialog_setup)                               #action to run setup_guy.py
        self.actionUser.triggered.connect(self.run_dialog_user_set)                              #action to run user_form.py
        self.actionClient_check.triggered.connect(self.run_dialog_client_read)              #action to rin client_read.py
        self.actionClient_add.triggered.connect(self.run_dialog_client_make)                #action to run client_make.py
        self.actionEquipment_add.triggered.connect(self.run_dialog_add_equipment)         #action to run add_equipment.py
        self.actionOrders.triggered.connect(self.run_dialog_comanda)                           #action to run comanda.py
        self.actionServices.triggered.connect(self.run_dialog_add_services)                    #action to run services.py
        self.actionList_data.triggered.connect(self.run_dialog_list_data)                     #action to run list_data.py
        self.move(QtGui.QApplication.desktop().screen().rect().center()- self.rect().center())     #center main windows


    def run_dialog_user_set(self):
        dialog = modules.user_form.user_set()
        dialog.show()
        dialog.accept()
        sys.exit(dialog.exec_())
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    # app.setQuitOnLastWindowClosed(False)
    # QtGui.QApplication.setQuitOnLastWindowClosed(False)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

我的第二个档案是:

import sys,os.path
from PyQt4 import QtGui, uic

class user_set(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        file_path = os.path.abspath("form_ui/user_form.ui")
        uic.loadUi(file_path, self)
        self.setFixedSize(self.size())
        self.move(QtGui.QApplication.desktop().screen().rect().center() - self.rect().center())

# if __name__ == '__main__':
#     app = QtGui.QApplication(sys.argv)
#     window = user_set()
#     window.show()
#     sys.exit(app.exec_())

我尝试过PYPL解决方案,并解决了应用错误。我尝试了他的最后一个解决方案,但没有结果,该应用程序正在关闭。我将设置一个YouTube视频链接,看看我的意思。

PS:furas解决方案我无法测试它,因为我不理解它,我是pyhton的新手而不是母语为英语的人,抱歉。

Youtube链接:https://youtu.be/9F2-5NVaqvQ

1 个答案:

答案 0 :(得分:0)

您已在两个类中连续两次初始化超类!

user_set超类更改为QtGui.QDialog

import sys,os.path
from PyQt4 import QtGui, uic

class user_set(QtGui.QDialog):
    def __init__(self):
        QtGui.QWidget.__init__(self) init super [1]
        super(user_set, self).__init__() init super [2] -> GET RID OF THIS !!
        file_path = os.path.abspath("form_ui/user_form.ui")
        uic.loadUi(file_path, self)
        self.setFixedSize(self.size())
        self.move(QtGui.QApplication.desktop().screen().rect().center() - self.rect().center())

您不应该在班级.show()功能中拨打__init__!而是创建另一种方法,您可以在其中创建对话框并从中调用窗口小部件

class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        file_path = os.path.abspath("form_ui/main_window.ui")
        uic.loadUi(file_path, self)
        self.statusBar().showMessage('Developer contact: admin.unu@protonmail.com')
        self.actionExit.triggered.connect(self.close)
        self.actionSetup.triggered.connect(setup.setup_gui.setupgui)
        self.actionUser.triggered.connect(self.run_dialog_user_set)
        self.actionClient_check.triggered.connect(modules.client_read.client_read_op)
        self.actionClient_add.triggered.connect(modules.client_make.client_make_op)
        self.actionEquipment_add.triggered.connect(modules.add_equipment.add_equipment_op)
        self.actionOrders.triggered.connect(modules.comanda.comanda_def)
        self.actionServices.triggered.connect(modules.services.add_services_op)
        self.actionList_data.triggered.connect(modules.list_data.list_data_op)
        self.move(QtGui.QApplication.desktop().screen().rect().center()- self.rect().center())

    def run_dialog_user_set(self):
        dialog = modules.user_form.user_set()
        dialog.show()