Python:在PyQt4之谜中嵌入Chaco

标签 python pyqt pyqt4 chaco

如何将 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 继承了什么?

最佳答案

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

我编写了一个独立的小示例来填充您的代码模板中的空白,并演示了如何在 Qt 窗口中嵌入查科图。

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()

关于Python:在PyQt4之谜中嵌入Chaco,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2148279/

相关文章:

python - pytest parametrize - 来自 CSV 的行作为测试用例

python - QComboBox调整下拉宽度

python - PyQt - 减少小部件*扩展*布局中的边距和间距

python - 使用 Pyqt4 从 URL 下载数千个 PDF

python - pandas 中的条件方法链接

python - Pandas 图中单行的访问和更改特征

python - Python 中枚举的枚举?

python - pyqt QTreeWidget setColumnWidth 不起作用

python - Qt - 在 Python 中保留小部件的引用

python - PyQt4:数据绑定(bind)?