python - 如何使嵌入式Python解释器的本地空间与全局空间共享一些变量

标签 python pyqt pyside

我在我的 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 文件,然后调用 graphene 函数来发出信号。

在主窗口类中,我有一个插槽。

 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

最佳答案

有几种机制。如果您有在某个点传递的内容,您可以使用 QtCore.Signal() 和 @QtCore.Slot() 。您可以将大多数内容放入 Signal 中,请参阅信号/槽示例。 Qt Signals & Slots Documentation

另一种我不太熟悉的机制是 QQueue类(class)。需要解决的问题是您正在跨线程传递数据,因此需要保护数据访问。

信号/槽示例:

class myDataType(QObject):
    def __init__(self, data):
       self.data = data
    ...

class foo(QObject):
    mySignal = QtCore.Signal(myDataType)
    def __init__(self):
    ...
    def someFunction(self, data):
        mySignal.emit(data)

class bar(QObject):
    def __init__(self):
        self.otherObject = foo()
        self.otherObject.mySignal.connect(self.handler)

    @QtCore.Slot(myDataType)
    def handler(self, data):
        do something with data

附录 1:

假设您有一个QMainWindow

class myMainWindow(QtGui.QMainWindow):
    mainWindowSignal = QtCore.Signal(QObject)
    def __init__(self, parent, *vargs, **kwargs):
        ...
        self.myCommandWidget = CommandWidget(parent=self)
        self.myCommandButton = QtGui.QPushButton("Press Me")

        #This connects the button being clicked to a function.
        self.myCommandButton.clicked.connect(self.button_pressed)

        #This connects the Signal we made 'mainWindowSignal' to
        # the do_something Slot in CommandWidget 'myCommandWidget'
        self.mainWindowSignal.connect(self.myCommandWidget.do_something)

        #This connects the Signal from 'myCommandWidget' 'dataReady'
        # to Slot 'data_returned' to handle the data
        self.myCommandWidget.dataReady.connect(self.dataReady)
        self.data = "some data"

    #We don't have to decorate this, but should
    @QtCore.Slot()
    def button_pressed(self):
        self.mainWindowsSignal.emit(self.data)

    @QtCore.Slot(str, int)
    def data_returned(self, strValue, intValue):
        #do something with the data.
        #e.g.
        self.command = strValue
        self.retCode = intValue




class CommandWidget(QWidget):
    dataReady = QtCore.Signal(str, int)
    def __init__(self, parent=None):
        #stuff you had here.
        ...
    @QtCore.Slot(QObject
    def do_something(self, data):
        retStr = self.buffer.... #insert your function calls here
        retInt = self.buffer.... 
        self.dataReady.emit(retValue, retInt)

关于python - 如何使嵌入式Python解释器的本地空间与全局空间共享一些变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37313690/

相关文章:

python - 合并 pandas.DataFrame 上的每两列

Python QWebView : Accessing Network Resources

python - 如何使用 pyQT 复制此 tkinter 滚动行为?

python - 如何从 pyside 中的 slider 获取值?

python - Mac Mountain Lion/Macports/Python27 : Installed, 已激活但导入错误

Python 文本文件到字典中不起作用

python - 我怎样才能取回一个类的动态创建的对象?

qt - 如何通过拖动 Widget 的角来调整 Widget 上的 QTableWidget 大小

c++ - Qt如何检查按下了哪个鼠标按钮

python - Scipysolve_banded矩阵使用的求解方法