【问题标题】:Open a GUI file from another file PyQT从另一个文件 PyQT 打开一个 GUI 文件
【发布时间】:2013-01-23 04:47:43
【问题描述】:

我使用 QT Designer 在 PyQT 中创建了许多 GUI 界面,但现在我试图从另一个界面打开一个界面,但我不知道该怎么做。 Start.py是运行GUI界面Authentification_1的文件,Acceuil_start.py是运行GUI界面Acceuil_2的文件。 py,现在我想从 Start.py 到午餐 Acceuil_start.py。 你对此有什么想法吗?谢谢你。 这是我的代码:

开始.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_())

【问题讨论】:

    标签: python qt python-3.x pyqt pyqt4


    【解决方案1】:

    首先,您应该命名您的 GUI 类,以便它们具有不同的名称,而不是通用名称,这样您就可以区分它们。

    您为什么需要这样做?好吧-简单地说,因为它有意义-如果每个类都代表不同类型的对话框,那么它就是不同的类型-并且应该以不同的方式命名。其中一些名称是/可能是:QMessageBoxAboutBoxAddUserDialog,等等。

    在 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() #???
    

    关于父级问题:如果您的小部件/窗口正在创建另一个小部件,将创建者对象设置为父级始终是一个好主意(除了一些特殊情况),您应该阅读this 以了解原因:

    QObjects 在对象树中组织自己。当您使用另一个对象作为父对象创建 QObject 时,它会添加到父对象的 children() 列表中,并在父对象存在时被删除。事实证明,这种方法非常适合 GUI 对象的需求。例如,QShortcut(键盘快捷键)是相关窗口的子窗口,因此当用户关闭该窗口时,该快捷键也会被删除。

    编辑 - 最小工作样本

    为了了解我想要告诉您的内容,我构建了一个简单的示例。你有两个班级 - MainWindowChildWindow。通过创建单独的 QApplication 对象,每个类都可以在没有其他类的情况下工作。但是,如果您在MainWindow 中导入ChildWindow,您将在连接到单次计时器的插槽中创建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_())
    

    【讨论】:

    • 谢谢。是父类,应该打开另一个类吗?以及为什么要更改 Acceuil_start.py 中的类名?
    • 不,它给了我这个错误:NameError: global name 'Acceuil' is not defined for : self.Acceuil = Acceuil(self)
    • 别忘了导入那个类:from Acceuil_start import Acceuil
    • 它给了我:NameError: global name 'self' is not defined
    • def authentifier(val): 更改为 def authentifier(self):
    【解决方案2】:

    要从 Start.py 引用另一个对话框,您必须在其前面加上模块名称,在本例中为 Acceuil_start。因此,如果每个模块中存在重复的函数名称是可以的。所以,你会:

    def authentifier(val): #Slot method
        dlg = Acceuil_start.StartQT4()
        dlg.exec_()
    

    但是,如果您希望它们从同一个进程运行,请记住您不能拥有两个 app 对象。您可能希望将 Acceuil_start.py 构造成一个对话框,而不是一个主窗口。如果这是两个不同的主窗口,您可能会发现只调用另一个 Python 解释器并以 Acceuil_start.py 作为参数会更容易。

    【讨论】:

    • 谢谢。它给了我AttributeError: 'Acceuil' object has no attribute 'exec_'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多