简体   繁体   English

从另一个文件PyQT打开一个GUI文件

[英]Open a GUI file from another file PyQT

I've created many GUI interfaces in PyQT using QT Designer, but now I'm trying to open an interface from another one, and I don't know how to do it.. Start.py is the file which run the GUI Interface Authentification_1 and Acceuil_start.py is the file which run the GUI interface Acceuil_2.py , now I want from Start.py to lunch Acceuil_start.py . 我使用QT Designer在PyQT中创建了许多GUI界面,但现在我正在尝试从另一个界面打开一个界面,我不知道该怎么做.. Start.py是运行GUI界面的文件Authentification_1Acceuil_start.py是运行GUI界面Acceuil_2.py的文件,现在我想从Start.pyAcceuil_start.py午餐。 Do you have any idea about that ? 你对此有什么想法吗? Thank you. 谢谢。 Here's my code : 这是我的代码:

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 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_())

First, you should to name your GUI classes so they have a different name, and not the generic one, so you could distinct them. 首先,您应该为GUI类命名,以便它们具有不同的名称,而不是通用名称,因此您可以区分它们。

Why you would need to do that? 为什么你需要这样做? Well - simply, because it makes sense - if every class is representing different type of dialog, so it is the different type - and it should be named differently. 嗯 - 简单地说,因为它是有意义的 - 如果每个类代表不同类型的对话,那么它是不同的类型 - 它应该以不同的名称命名。 Some of the names are/may be: QMessageBox , AboutBox , AddUserDialog , and so on. 一些名称是/可能是: QMessageBoxAboutBoxAddUserDialog等。

In Acceuil_start.py (you should rename class in other module, too). 在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_())

in the parent class, when you want to create the window, you are close (but it should work in any case): 在父类中,当你想要创建窗口时,你就近了(但它应该在任何情况下都有效):

def authentifier(val): #Slot method
    self.Acceuil = Acceuil(self) # You should always pass the parent to the child control
    self.Acceuil.show() #???

About parent issue: If your widget/window is creating another widget, setting creator object to be parent is always a good idea (apart from some singular cases), and you should read this to see why is that so: 关于父问题:如果您的窗口小部件/窗口正在创建另一个窗口小部件,将创建者对象设置为父窗口总是一个好主意(除了一些单个案例),您应该阅读此内容以了解为什么会这样:

QObjects organize themselves in object trees. QObjects在对象树中组织自己。 When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is. 当您使用另一个对象作为父对象创建QObject时,它将添加到父对象的children()列表中,并在父对象时删除。 It turns out that this approach fits the needs of GUI objects very well. 事实证明,这种方法很好地满足了GUI对象的需求。 For example, a QShortcut (keyboard shortcut) is a child of the relevant window, so when the user closes that window, the shorcut is deleted too. 例如,QShortcut(键盘快捷键)是相关窗口的子项,因此当用户关闭该窗口时,也会删除shorcut。

Edit - Minimal Working Sample 编辑 - 最小工作样本

To see what I am trying to tell you, I've built a simple example. 为了看看我想告诉你什么,我已经建立了一个简单的例子。 You have two classes - MainWindow and ChildWindow . 你有两个类 - MainWindowChildWindow Every class can work without other class by creating separate QApplication objects. 通过创建单独的QApplication对象,每个类可以在没有其他类的情况下工作 But, if you import ChildWindow in MainWindow , you will create ChildWindow in slot connected to singleshot timer which will trigger in 5 seconds. 但是,如果你在MainWindow导入ChildWindow ,你将在连接到singleshot计时器的插槽中创建ChildWindow ,它将在5秒内触发。

MainWindow.py: 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: 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_())

To reference the other dialog from Start.py, you must prefix it by the module name, which in this case is Acceuil_start. 要从Start.py引用其他对话框,必须在模块名称前加上前缀,在本例中为模块名称,即Acceuil_start。 As such, it is OK if there are duplicated function names in each module. 因此,如果每个模块中存在重复的函数名称,则可以。 So, you would have: 所以,你会有:

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

However, if you want these to run from the same process, keep in mind that you can't have two app objects. 但是,如果您希望这些操作从同一进程运行,请记住您不能拥有两个app对象。 You would probably want to structure Acceuil_start.py to act like a dialog, rather than a main window. 您可能希望将Acceuil_start.py构建为对话框,而不是主窗口。 If these are two distinct main windows, you might find it easier to just invoke another Python interpreter with the Acceuil_start.py as a parameter. 如果它们是两个不同的主窗口,您可能会发现使用Acceuil_start.py作为参数调用另一个Python解释器更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM