从另一个文件PyQT打开GUI文件
问题内容:
我已经使用QT Designer在PyQT中创建了许多GUI界面,但是现在我试图从另一个界面中打开一个界面,但我不知道该怎么做 。Start.py
是运行GUI界面的文件 Authentification_1 和 Acceuil_start.py 是运行GUI界面
Acceuil_2.py 的文件,现在我要从 Start.py 到午餐 Acceuil_start.py
。您对此有什么想法吗?谢谢。这是我的代码:
Start.py:
import sys
from PyQt4 import QtCore, QtGui
from Authentification_1 import Ui_Fenetre_auth
from Acceuil_2 import Ui_MainWindow #??? Acceuil_2.py is the file which I want to open
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Fenetre_auth()
self.ui.setupUi(self)
def authentifier(val): #Slot method
self.Acceuil = Acceuil() #???
self.Acceuil.show() #???
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
Acceuil_start.py
import sys
from PyQt4 import QtCore, QtGui
from Authentification_1 import Ui_Fenetre_auth
from Acceuil_2 import Ui_MainWindow
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
问题答案:
首先,您应该命名您的GUI类,以便它们具有不同的名称,而不是通用名称,因此可以区分它们。
为什么您需要这样做?好吧-简单地说,因为这很有意义-如果每个类都代表不同类型的对话框,那么它也就是不同的类型-
并且应该以不同的方式命名。一些名字是/可能是:QMessageBox
,AboutBox
,AddUserDialog
,等等。
在Acceuil_start.py中(您也应该在其他模块中重命名类)。
import sys
from PyQt4 import QtCore, QtGui
from Authentification_1 import Ui_Fenetre_auth
from Acceuil_2 import Ui_MainWindow
class Acceuil(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = Acceuil()
myapp.show()
sys.exit(app.exec_())
在父类中,当您要创建窗口时,您已经关闭(但是无论如何它都可以工作):
def authentifier(val): #Slot method
self.Acceuil = Acceuil(self) # You should always pass the parent to the child control
self.Acceuil.show() #???
关于父级问题:如果您的窗口小部件/窗口正在创建另一个窗口小部件,则将创建者对象设置为父级始终是一个好主意(除了某些特殊情况),您应该阅读以下内容以了解原因:
QObject将自己组织在对象树中。当您创建一个以另一个对象作为父对象的QObject时,它将被添加到父对象的children()列表中,而当父对象为父对象时将其删除。事实证明,这种方法非常适合GUI对象的需求。例如,QShortcut(键盘快捷键)是相关窗口的子级,因此,当用户关闭该窗口时,该快捷方式也会被删除。
编辑-最小工作样本
为了了解我要告诉您的内容,我建立了一个简单的示例。您有两个课程-MainWindow
和
ChildWindow
。通过创建单独的QApplication
对象,每个类都可以在没有其他类的情况下工作。但是,如果导入ChildWindow
中MainWindow
,您将创建ChildWindow
在连接到singleshot的计时器,它会在5秒内触发插槽。
MainWindow.py:
import sys
from PyQt4 import QtCore, QtGui
from ChildWindow import ChildWindow
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
QtCore.QTimer.singleShot(5000, self.showChildWindow)
def showChildWindow(self):
self.child_win = ChildWindow(self)
self.child_win.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
ChildWindow.py:
import sys
from PyQt4 import QtCore, QtGui
class ChildWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setWindowTitle("Child Window!")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = ChildWindow()
myapp.show()
sys.exit(app.exec_())