【问题标题】:Python: Embed Chaco in PyQt4 MysteryPython:将 Chaco 嵌入 PyQt4 之谜
【发布时间】:2011-01-10 00:50:31
【问题描述】:

如何将 Chaco 添加到现有的 PyQt4 应用程序中?

数小时的搜索收效甚微 (search for yourself)。到目前为止,我认为我需要以下几行:

import os
os.environ['ETS_TOOLKIT']='qt4'

我在互联网上的任何地方都找不到 PyQt4-Chaco 代码

我将非常感谢任何填写空白的人向我展示可能的最简单的线图(2 分)

from PyQt4 import QtCore, QtGui
import sys
import os
os.environ['ETS_TOOLKIT']='qt4'

from enthought <blanks>
:
:

app = QtGui.QApplication(sys.argv)
main_window = QtGui.QMainWindow()
main_window.setCentralWidget(<blanks>)
main_window.show()
app.exec_()
print('bye')

Chaco/Enthought 类继承自 QWidget 什么?

【问题讨论】:

    标签: python pyqt pyqt4 chaco


    【解决方案1】:

    我不认识 Chaco,但快速浏览告诉我这是不可能的。

    Chaco 和 PyQt 都是设计用于与用户交互的图形工具包。 Chaco 是面向情节的,而 PyQt 更面向应用程序。每个人都有自己的方式来管理什么是窗口、如何检测用户点击、如何处理绘制事件……这样它们就不会混在一起。

    如果需要绘图软件,可以尝试使用matplotlib生成图形的静态图像并在PyQt中显示。或者尝试基于 PyQt 的图形或绘图工具包。

    【讨论】:

    • 我发现 matplotlib 1. 对我的需求来说太慢了 2. 不像 chaco 那样“支持交互” 3. 它的设计很古怪。
    • 我知道 Chaco 可以使用 PyQt(和 wx)。不幸的是,Chaco 缺乏关于这个主题和其他主题的良好文档
    【解决方案2】:

    我不了解 Chaco,但我正在使用 VTK,这里是绘制一些线条的代码,它们具有 (x,y,z) 坐标。

        """Define an actor and its properties, to be drawn on the scene using 'lines' representation."""
        ren = vtk.vtkRenderer()
        apd=vtk.vtkAppendPolyData()
    
        for i in xrange(len(coordinates)):
            line=vtk.vtkLineSource()
    
            line.SetPoint1(coordinates[i][0]) # 1st atom coordinates for a given bond
            line.SetPoint2(coordinates[i][1]) # 2nd atom coordinates for a given bond
            line.SetResolution(21)
            apd.AddInput(line.GetOutput())
    
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInput(apd.GetOutput())
        lines_actor = vtk.vtkActor()
        lines_actor.SetMapper(mapper)
        lines_actor.GetProperty().SetColor(colorR, colorG, colorB)
        lines_actor.GetProperty().SetOpacity(opacity)
    
            # Add newly created actor to the renderer.
            self.ren.AddViewProp(actor) # Prop is the superclass of all actors, composite props etc.
            # Update renderer.
            self.ren.GetRenderWindow().Render()
    

    它使用 QVTKRenderWindowInteractor 与 PyQT4 交互。

    【讨论】:

      【解决方案3】:

      这是你需要的:

      import os, sys
      os.environ['ETS_TOOLKIT'] = 'qt4'
      
      from PyQt4 import QtGui
      app = QtGui.QApplication(sys.argv)
      from numpy import linspace, pi, sin
      from enthought.enable.api import Component, Container, Window
      from enthought.chaco.api import create_line_plot, \
                                      add_default_axes, \
                                      add_default_grids, \
                                      OverlayPlotContainer
      
      
      x = linspace(-pi,pi,100)
      y = sin(x)
      plot = create_line_plot((x,y))
      add_default_grids(plot)
      add_default_axes(plot)
      container = OverlayPlotContainer(padding = 50)
      container.add(plot)
      plot_window = Window(None, -1, component=container)
      plot_window.control.setWindowTitle('hello')
      plot_window.control.resize(400,400)
      plot_window.control.show()
      
      app.exec_()
      

      plot_window.control 继承自 QWidget

      【讨论】:

      • 注意:app = QtGui.QApplication(sys.argv) 行必须在任何考虑导入之前出现
      【解决方案4】:

      我今天才看到这个。将 Chaco 嵌入到 Qt 和 WX 中是绝对可行且相当简单的。事实上,所有的例子,当你的 ETS_TOOLKIT 环境变量设置为“qt4”时,都是这样做的。 (Chaco 要求有一个底层的 GUI 工具包。)

      我编写了一个小的独立示例,它填补了代码模板中的空白,并演示了如何在 Qt 窗口中嵌入 chaco Plot。

      qt_example.py:

      """
      Example of how to directly embed Chaco into Qt widgets.
      
      The actual plot being created is drawn from the basic/line_plot1.py code.
      """
      
      import sys
      from numpy import linspace
      from scipy.special import jn
      from PyQt4 import QtGui, QtCore
      
      from enthought.etsconfig.etsconfig import ETSConfig
      ETSConfig.toolkit = "qt4"
      from enthought.enable.api import Window
      
      from enthought.chaco.api import ArrayPlotData, Plot
      from enthought.chaco.tools.api import PanTool, ZoomTool
      
      
      class PlotFrame(QtGui.QWidget):
          """ This widget simply hosts an opaque enthought.enable.qt4_backend.Window
          object, which provides the bridge between Enable/Chaco and the underlying
          UI toolkit (qt4).  This code is basically a duplicate of what's in
          enthought.enable.example_support.DemoFrame, but is reproduced here to
          make this example more stand-alone.
          """
          def __init__(self, parent, **kw):
              QtGui.QWidget.__init__(self)
      
      def create_chaco_plot(parent):
          x = linspace(-2.0, 10.0, 100)
          pd = ArrayPlotData(index = x)
          for i in range(5):
              pd.set_data("y" + str(i), jn(i,x))
      
          # Create some line plots of some of the data
          plot = Plot(pd, title="Line Plot", padding=50, border_visible=True)
          plot.legend.visible = True
          plot.plot(("index", "y0", "y1", "y2"), name="j_n, n<3", color="red")
          plot.plot(("index", "y3"), name="j_3", color="blue")
      
          # Attach some tools to the plot
          plot.tools.append(PanTool(plot))
          zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
          plot.overlays.append(zoom)
      
          # This Window object bridges the Enable and Qt4 worlds, and handles events
          # and drawing.  We can create whatever hierarchy of nested containers we
          # want, as long as the top-level item gets set as the .component attribute
          # of a Window.
          return Window(parent, -1, component = plot)
      
      def main():
          app = QtGui.QApplication(sys.argv)
          main_window = QtGui.QMainWindow(size=QtCore.QSize(500,500))
      
          enable_window = create_chaco_plot(main_window)
      
          # The .control attribute references a QWidget that gives Chaco events
          # and that Chaco paints into.
          main_window.setCentralWidget(enable_window.control)
      
          main_window.show()
          app.exec_()
      
      if __name__ == "__main__":
          main()
      

      【讨论】:

        猜你喜欢
        • 2011-11-26
        • 2017-01-30
        • 2015-04-16
        • 2017-11-23
        • 1970-01-01
        • 2014-01-22
        • 1970-01-01
        • 1970-01-01
        • 2011-08-20
        相关资源
        最近更新 更多