【发布时间】:2016-05-26 21:12:40
【问题描述】:
我在我的 pyside 程序中编写了一个命令行小部件。结构是这样的:
class CommandWidget(QWidget):
def __init__(self, parent = None):
super(CommandWidget, self).__init__(parent)
self.buffer=PyInterp(self)
self.buffer.initInterpreter(locals())
self........
class PyInterp(QTextEdit):
class InteractiveInterpreter(code.InteractiveInterpreter):
def __init__(self, locals):
code.InteractiveInterpreter.__init__(self, locals)
def runIt(self, command):
code.InteractiveInterpreter.runsource(self, command)
def __init__(self, parent = None):
super(PyInterp, self).__init__(parent)
我还有一个与其他小部件一起运行的主窗口程序。我的问题是如何在这个解释器的其他小部件类中导入一些函数,运行函数并将结果输出到全局空间。或者换句话说,我想在解释器的本地空间和全局主窗口空间之间共享一些变量。我怎样才能做到这一点?
编辑: 这是我要放入信号的数据类型。
class PosType(QObject):
def __init__(self, nx, ny, nz, start_pos, type):
self.nx = nx
self.ny = ny
self.nz = nz
self.start_pos = start_pos
self.type = type
这是信号。
class PosSig(QObject):
sig = Signal(PosType)
def emit_sig(self, pos_data):
self.sig.emit(pos_data)
这是我想放入解释器的函数,这样当它被调用时会发出一个信号。
def graphene(nx, ny, start_pos):
pos_info = PosType(nx = nx, ny = ny, nz = None, start_pos = start_pos, type = 1)
tmp_sig = PosSig()
tmp_sig.emit_sig(pos_info)
return
上面的类都在一个名为 ExposeFunc.py 的文件中,我打算在解释器中导入这个 .py 文件,然后调用石墨烯函数发出信号。
在主窗口类中,我有一个插槽。
def __init__(self):
#Interpreter Signals :
possig = PosSig()
possig.sig.connect(self.createObject)
@Slot(PosType)
def createObject(self, pos_info):
type = pos_info.type
if type == 1:
SharedItems.QS._FillData(pos_info.nx, pos_info.ny, start_pos)
return
【问题讨论】: