我有一个python project.and,我是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
发布于 2017-01-13 22:33:27
您已经在两个类中连续两次初始化了超类!
将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())您不应该在类__init__函数中调用.show()!而是创建另一个方法,在该方法中创建对话框并从中调用小部件
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()https://stackoverflow.com/questions/41634044
复制相似问题