将文件中的 UI 个对象调用到另一个文件中

Calling UI objects from a file into another

我目前正在 Python 中为 Maya 编写 UI 脚本。

所以,我的 UI 在顶部有不同的选项卡,我不想将每一段代码都放在 MainClass 中,因为那样会太乱太长。对于每个选项卡,我想将其脚本编写在不同的 .py 文件中。我想在 __init__ 函数下创建连接,同时将另一个脚本中的函数加载到这个 MainClass 中以供使用。

问题是,我应该如何在新文件中从 UI 调用 objectName?我尝试导入 MainClass 代码,但没有用,我不想在新的 .py 文件中初始化 UI window。解决这个问题的好方法是什么?

编辑

示例:

test.ui 文件有一个标记为 "Print" 的按钮和一个列表小部件。每次按下 'Print' 按钮,单词 "Hello World" 将出现在列表小部件上。

在loadUi_test.py文件中

def loadUi(uiFile):
    #code that loads ui

def getMayaWindow():
    #gets main Maya Window

    ptr = apiUI.MQtUtil.mainWindow()
    if ptr is not None:
        return shiboken.wrapInstance(long(ptr), QtGui.QMainWindow)

class mainClass():
    def __init__(self, parent = getMayaWindow()):

        super(pipeWindow,self).__init__(parent)
        self.setupUi(self)

    def closeEvent(self,event):
        super(mainClass, self).closeEvent(event)

在function_test.py

def printFunc():
    listWidget.clear()
    listWidget.addItem("Hello World!")

init.py

from pipeline import loadUi_test
from pipeline import function_test

uiFile = "test.ui"
b = loadUi_test.loadUi(uiFile)
a = loadUi_test.mainClass()
a.pushButton.clicked.connect(function_test.printFunc(b))

这不起作用,我收到错误消息“元组对象没有属性 listWidget”

如果我这样做:a.pushButton.clicked.connect(function_test.printFunc(a)),我会得到错误 "Failed to connect signal clicked()"

通常,您可以从其他文件加载 classes,只要所有文件都在您的 python 路径上。您可以使用 import 语句加载 class。

典型模式的基本示例在磁盘上如下所示

mytool
|
+---  __init__.py
+--- model_tab.py
+--- texture_tab.py
+--- tool_tab.py

其中主要工具是 mytool,在 __init__.py 中定义,组件位于其他文件中。您可以使用 from SomeModule import SomeClass 模式导入 class。这样可以轻松地将您的作品保存在单独的文件中。您将在 __init__.py

中像这样构建导入
from mytool.model_tab import ModelTab
from mytoool.texture_tab import TextureTab
from mytool.tool_tab import ToolTab

有了它,您可以 assemble 使用您在其他文件中定义的 classes 的实际 gui。您可以将您的 master class 放在 __init__.py 或单独的文件中,这看起来很方便