Python 另一个py文件中的pyqt调用ui

Python 另一个py文件中的pyqt调用ui,python,qt,pyqt,Python,Qt,Pyqt,我使用qtDesigner和uic来获取ui.py文件,我想调用Output.py文件中的ui变量 当我运行演示时,它显示为Nameerror。可能是什么问题 提前谢谢! 从QtDesigner导出有一个类定义(类似于此): 将ui.py导入到另一个文件时,您可以执行以下操作: from PyQt4.QtGui import QDialog from ui import Ui_Form # ui because of ui.py, Ui_Form because of the class na

我使用qtDesigner和uic来获取ui.py文件,我想调用Output.py文件中的ui变量

当我运行演示时,它显示为Nameerror。可能是什么问题

提前谢谢!

从QtDesigner导出有一个类定义(类似于此):

ui.py
导入到另一个文件时,您可以执行以下操作:

from PyQt4.QtGui import QDialog
from ui import Ui_Form  # ui because of ui.py, Ui_Form because of the class name

class AppWindow(QDialog):

    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.show()
        # more definitions (buttons etc.) come here, example follows:
        self.ui.ExistingButton.clicked.connect(self.pressed_okay)

    def pressed_okay(self):
        self.accept()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = AppWindow()
    w.show()
    sys.exit(app.exec_())

这是启动GUI的最低要求。

将问题中的实际代码和完整的回溯一起发布。感谢您的帮助,我找到了原因。感谢您的回答。我想呼叫ui的成员,例如ui.button。您的回答创建了一个新对象。您的
\uuu init\uuu
似乎不正确。调用
super(AppWindow,self)。\uuuu init\uuuuu()
QDialog.\uuuuu init\uuuuuuuuuu(self)
。我从我的一个程序中复制了它,实际上它正在工作。我知道有几种方法可以执行
\uuuu init\uuu
。我想这是从YouTube教程上得到的@灰姑娘:如果你想在
\uuuuu init\uuuuuuu
中调用你的按钮,例如
self.ui.button.clicked.connect(self.function\u that\u does)
谢谢你的帮助,我找到了原因:如果name\uuuu==“\uu main”。因此,其他模块无法在其中调用代码。我创建一个新对象并继承ui对象,它工作得很好
from PyQt4.QtGui import QDialog
from ui import Ui_Form  # ui because of ui.py, Ui_Form because of the class name

class AppWindow(QDialog):

    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.show()
        # more definitions (buttons etc.) come here, example follows:
        self.ui.ExistingButton.clicked.connect(self.pressed_okay)

    def pressed_okay(self):
        self.accept()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = AppWindow()
    w.show()
    sys.exit(app.exec_())