0

我正在尝试使用从 PySide 启动的 chaco 制作一个 numpy 图像滑块。我试过使用 matplotlib,但它缺乏更新速度,所以我一直在尝试 chaco。下面的脚本首先初始化一个 Pyside 对话框,从这里我试图打开一个 chaco/traits 窗口,该窗口用于显示 numpy 数组的不同视图,使用滑块像电影一样滚动数组。该脚本有效,但我收到一条警告消息:

QCoreApplication::exec: The event loop is already running 

我想我可能需要使用自己的线程或其他东西来启动 chaco 窗口,但不知道如何去做。任何帮助将不胜感激,脚本如下:

import numpy as np
import sys
from PySide.QtCore import *
from PySide.QtGui import *

app = QApplication(sys.argv)

from traits.api import *
from traitsui.api import *
from enable.api import *
from enable.component_editor import *
from chaco.api import *
from chaco.tools.api import *
from chaco import *


class chacoWindow(HasTraits):
    #Chaco popup window with slider
    plot = Instance(HPlotContainer)
    #Slider max value is set using Frame
    Frame = Int
    high = 100

    traits_view = View(Group(
                    Item('plot', 
                             editor=ComponentEditor(), 
                             show_label=False),
                    Item( 'Frame', 
                             editor = RangeEditor( 
                             high_name   = 'high',
                             format      = '%d',
                             is_float    = False))),
                   width=1000, height=600, resizable=True, title="Track viewer")

    def __init__(self, dataDict, arg1):

        self.high = arg1 - 1
        self.dataDict = dataDict

        self.data1 = self.dataDict['Movie1']
        self.data2 = self.dataDict['Movie2']

        #Left plot
        self.plotdata1 = ArrayPlotData(image1 = self.data1['0'])
        plotLeft = Plot(self.plotdata1)
        plotLeft.img_plot("image1")               
        #Right plot  
        self.plotdata2 = ArrayPlotData(image2 = self.data2['0'])
        plotRight = Plot(self.plotdata2)
        plotRight.img_plot("image2")

        #Add some zoom/pan tools
        plotLeft.tools.append(PanTool(plotLeft))
        plotLeft.tools.append(BetterZoom(plotLeft, zoom_factor=0.8, zoom_to_mouse = True))
        plotLeft.tools.append(DragZoom(plotLeft, drag_button="right"))   
        plotRight.tools.append(PanTool(plotRight))
        plotRight.tools.append(BetterZoom(plotRight))
        plotRight.tools.append(DragZoom(plotRight, drag_button="right"))
        #Axes shared          
        plotLeft.range2d = plotRight.range2d
        container = HPlotContainer(plotLeft, plotRight)
        self.plot = container

    def _Frame_changed(self):
        #Change frame when slider changes
        self.plotdata1.set_data("image1", self.data1[str(self.Frame)])
        self.plotdata2.set_data("image2", self.data2[str(self.Frame)])


class Form(QDialog):
    #Basic form dialog using Pyside
    def __init__(self, dataDict, parent=None):
        super(Form, self).__init__(parent)
        self.dataDict = dataDict 
        self.totalFrames = len(dataDict['Movie1'])
        formatButton1 = QPushButton("Format 1")        
        buttonLayout = QHBoxLayout()
        buttonLayout.addStretch()
        buttonLayout.addWidget(formatButton1)
        #Add VBox space for extra widgets
        layout = QVBoxLayout()        
        layout.addLayout(buttonLayout)
        self.setLayout(layout)
        self.connect(formatButton1, SIGNAL("clicked()"), self.setChaco)                     
        self.setWindowTitle('Master form')

    def setChaco(self):
        #Create chaco window instance
        scatter = chacoWindow(dataDict, self.totalFrames)
        scatter.configure_traits(self) 


if __name__ == "__main__":       
    #make some demo data
    ar0 = np.zeros((25,25))
    ar0[10][10] = 1
    ar1 = np.zeros((25,25))
    ar1[12][12] = 1    
    #Dictionary of images
    dataDict = {'Movie1': {'0': ar0, '1':ar1, '2':ar0}, 
                'Movie2': {'0': ar1, '1':ar0, '2':ar1}}
    form = Form(dataDict)
    form.show()
    app.exec_()
4

1 回答 1

0

我设法找到了解决方案。更改此功能可解决问题:

def setChaco(self):
    #Create chaco window instance
    scatter = chacoWindow(dataDict, self.totalFrames)
    #Change this line: scatter.configure_traits(self)
    scatter.edit_traits() 

这似乎让 PySide 事件循环处理所有事情。

于 2014-06-10T09:24:07.307 回答