从pyside开始chaco-traits弹出窗口的新事件循环?

时间:2014-06-09 22:01:41

标签: python multithreading pyside traits chaco

我正在尝试使用从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_()

1 个答案:

答案 0 :(得分:0)

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

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

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