将用户界面对象从文件调用到另一个文件 [英] Calling UI objects from a file into another
问题描述
我目前正在用Python为Maya编写UI脚本.
I am currently writing a UI script for Maya in Python.
因此,我的UI顶部具有不同的选项卡,并且我不想将每个代码都放在MainClass
中,因为这太麻烦且太长了.对于每个选项卡,我想在不同的.py
文件中编写其脚本.我想在__init__
函数下创建连接,同时,将另一个脚本中的函数加载到要使用的MainClass
中.
So, I have UI that has different tabs at the top and I do not want to put every single piece of code in the MainClass
because that would be too messy and long. For every tab, I want to write its script in a different .py
file. I want to create the connections under the __init__
function, at the same time, load functions from another script into this MainClass
to be used.
问题是,我应该如何在新文件中从UI调用objectName?我试图导入MainClass
代码,但是没有用,并且我不想在新的.py
文件中初始化UI窗口.解决这个问题的好方法是什么?
Question is, how should I go about calling objectName from the UI in a new file? I tried to import the MainClass
code but that didn't work and I don't want the initialize the UI window in the new .py
file. What's a good way to go about this?
编辑
示例:
test.ui文件具有一个标记为打印"的按钮和一个列表小部件.每次按下打印"按钮,列表小部件上都会出现"Hello World"字样.
test.ui file has one button labelled "Print" and a list Widget. Every time 'Print' button is pressed, the words "Hello World" will appear on the list widget.
在loadUi_test.py文件中
In loadUi_test.py file
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
In 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"
This does not work, I get an error " tuple object has no attribute listWidget "
如果我改为这样做:a.pushButton.clicked.connect(function_test.printFunc(a))
,则会收到错误无法连接信号clicked()"
If I do this instead: a.pushButton.clicked.connect(function_test.printFunc(a))
, I get the error "Failed to connect signal clicked()"
推荐答案
通常,只要所有文件在python路径上都可用,就可以从其他文件加载类.您可以使用import语句加载类.
In general, you can load classes from other files as long as all of the files are available on your python path. You can load a class using the import statement.
典型模式的基本示例在磁盘上看起来像这样
A basic example of the typical pattern looks like this on disk
mytool
|
+--- __init__.py
+--- model_tab.py
+--- texture_tab.py
+--- tool_tab.py
其中的主要工具是mytool
,在__init__.py
中定义,而组件则位于其他文件中.您可以使用from SomeModule import SomeClass
模式导入类.这样可以很容易地将您的作品保存在单独的文件中.您可以在__init__.py
where the main tool is mytool
, defined in the __init__.py
and the component pieces live in the other files. You can import a class using the from SomeModule import SomeClass
pattern. That makes it easy to keep your pieces in separate files. You'd structure the imports like this in __init__.py
from mytool.model_tab import ModelTab
from mytoool.texture_tab import TextureTab
from mytool.tool_tab import ToolTab
有了这个,您就可以使用在其他文件中定义的类来汇编实际的gui.您可以将主类放在__init__.py
中,也可以放在方便的单独文件中
With that in place you can assemble your actual gui using the classes you have defined in your other files. You could put you master class in the __init__.py
or in a separate file as seems convenient
这篇关于将用户界面对象从文件调用到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!