[英]Open a GUI file from another file PyQT
我使用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() #???
关于父问题:如果您的窗口小部件/窗口正在创建另一个窗口小部件,将创建者对象设置为父窗口总是一个好主意(除了一些单个案例),您应该阅读此内容以了解为什么会这样:
QObjects在对象树中组织自己。 当您使用另一个对象作为父对象创建QObject时,它将添加到父对象的children()列表中,并在父对象时删除。 事实证明,这种方法很好地满足了GUI对象的需求。 例如,QShortcut(键盘快捷键)是相关窗口的子项,因此当用户关闭该窗口时,也会删除shorcut。
编辑 - 最小工作样本
为了看看我想告诉你什么,我已经建立了一个简单的例子。 你有两个类 - MainWindow
和ChildWindow
。 通过创建单独的QApplication
对象,每个类可以在没有其他类的情况下工作 但是,如果你在MainWindow
导入ChildWindow
,你将在连接到singleshot计时器的插槽中创建ChildWindow
,它将在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_())
要从Start.py引用其他对话框,必须在模块名称前加上前缀,在本例中为模块名称,即Acceuil_start。 因此,如果每个模块中存在重复的函数名称,则可以。 所以,你会有:
def authentifier(val): #Slot method
dlg = Acceuil_start.StartQT4()
dlg.exec_()
但是,如果您希望这些操作从同一进程运行,请记住您不能拥有两个app
对象。 您可能希望将Acceuil_start.py构建为对话框,而不是主窗口。 如果它们是两个不同的主窗口,您可能会发现使用Acceuil_start.py作为参数调用另一个Python解释器更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.