从另一个文件 PyQT 打开一个 GUI 文件 [英] Open a GUI file from another file PyQT

查看:60
本文介绍了从另一个文件 PyQT 打开一个 GUI 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<块引用>

开始.py:

导入系统从 PyQt4 导入 QtCore、QtGui从 Authentification_1 导入 Ui_Fenetre_auth从 Acceuil_2 导入 Ui_MainWindow #???Acceuil_2.py 是我要打开的文件类 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 方法self.Acceuil = Acceuil() #???self.Acceuil.show() #???如果 __name__ == "__main__":app = QtGui.QApplication(sys.argv)myapp = StartQT4()myapp.show()sys.exit(app.exec_())

<块引用>

Acceuil_start.py

导入系统从 PyQt4 导入 QtCore、QtGui从 Authentification_1 导入 Ui_Fenetre_auth从 Acceuil_2 导入 Ui_MainWindow类 StartQT4(QtGui.QMainWindow):def __init__(self, parent=None):QtGui.QWidget.__init__(self, parent)self.ui = Ui_MainWindow()self.ui.setupUi(self)如果 __name__ == "__main__":app = QtGui.QApplication(sys.argv)myapp = StartQT4()myapp.show()sys.exit(app.exec_())

解决方案

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

为什么你需要这样做?嗯 - 很简单,因为它是有道理的 - 如果每个类都代表不同类型的对话框,那么它就是不同的类型 - 它应该以不同的方式命名.一些名称是/可能是:QMessageBoxAboutBoxAddUserDialog 等等.

在 Acceuil_start.py 中(您也应该重命名其他模块中的类).

导入系统从 PyQt4 导入 QtCore、QtGui从 Authentification_1 导入 Ui_Fenetre_auth从 Acceuil_2 导入 Ui_MainWindow类 Acceuil(QtGui.QMainWindow):def __init__(self, parent=None):QtGui.QWidget.__init__(self, parent)self.ui = Ui_MainWindow()self.ui.setupUi(self)如果 __name__ == "__main__":app = QtGui.QApplication(sys.argv)myapp = Acceuil()myapp.show()sys.exit(app.exec_())

在父类中,当你想创建窗口时,你已经关闭了(但它应该在任何情况下都可以工作):

def authentifier(val): #Slot 方法self.Acceuil = Acceuil(self) # 你应该总是将父控件传递给子控件self.Acceuil.show() #???

关于父级问题:如果您的小部件/窗口正在创建另一个小部件,则将创建者对象设置为父级始终是一个好主意(除了一些个别情况),您应该阅读 这个 看看为什么会这样:

<块引用>

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

编辑 - 最小工作样本

为了了解我想告诉您的内容,我构建了一个简单的示例.你有两个类 - MainWindow 和<代码>子窗口.通过创建单独的 QApplication 对象,每个类都可以在没有其他类的情况下工作.但是,如果您在 MainWindow 中导入 ChildWindow,您将在连接到单次定时器的插槽中创建 ChildWindow,该定时器将在 5 秒内触发.

MainWindow.py:

导入系统从 PyQt4 导入 QtCore、QtGui从 ChildWindow 导入 ChildWindow类 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()如果 __name__ == "__main__":app = QtGui.QApplication(sys.argv)myapp = MainWindow()myapp.show()sys.exit(app.exec_())

ChildWindow.py:

导入系统从 PyQt4 导入 QtCore、QtGui类 ChildWindow(QtGui.QMainWindow):def __init__(self, parent=None):QtGui.QWidget.__init__(self, parent)self.setWindowTitle("子窗口!")如果 __name__ == "__main__":app = QtGui.QApplication(sys.argv)myapp = ChildWindow()myapp.show()sys.exit(app.exec_())

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. Do you have any idea about that ? Thank you. Here's my code :

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

解决方案

First, you should to name your GUI classes so they have a different name, and not the generic one, so you could distinct them.

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.

In Acceuil_start.py (you should rename class in other module, too).

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. 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. It turns out that this approach fits the needs of GUI objects very well. 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.

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. Every class can work without other class by creating separate QApplication objects. But, if you import ChildWindow in MainWindow, you will create ChildWindow in slot connected to singleshot timer which will trigger in 5 seconds.

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

这篇关于从另一个文件 PyQT 打开一个 GUI 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆