【问题标题】:Print Gnuplot to Kivy Widget将 Gnuplot 打印到 Kivy 小部件
【发布时间】:2019-05-08 20:55:26
【问题描述】:

我正在寻找实现这个 MWE:

https://stackoverflow.com/a/44922317/6924364

但是使用 Gnuplot,类似这样:

https://stackoverflow.com/a/21633082/6924364

如何将 Gnuplot 输出传递到 Kivy 应用程序,并显示图像而无需在本地保存?


编辑 1:这是一个 MWE,只需要一些代码即可将图像对象添加到自定义小部件,但我不确定如何完成。

from subprocess import Popen, PIPE
from StringIO import StringIO                                            
from io import BytesIO
from os import linesep as nl
from PIL import Image as Image

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.core.image import Image as CoreImage


def gnuplot(commands, data):                                                    
    """ drive gnuplot, expects lists, returns stdout as string """              

    dfile = StringIO()                                                          
    for line in data:                                                           
        dfile.write(str(line) + nl)                                             

    args = ["gnuplot", "-e", (";".join([str(c) for c in commands]))]            
    p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)                       

    dfile.seek(0)                                                               
    return p.communicate(dfile.read())[0]   


def gnuplot_GifTest():
    commands = [\
        "set datafile separator ','",\
        "set terminal png",\
        "set output",\
        "plot '-' using 1:2 with linespoints, '' using 1:2 with linespoints",\
        ]
    data = [\
        "1,1",\
        "2,2",\
        "3,5",\
        "4,2",\
        "5,1",\
        "e",\
        "1,5",\
        "2,4",\
        "3,1",\
        "4,4",\
        "5,5",\
        "e",\
        ]

    return (commands, data)


class Plot(Widget):

    (commands, data) = gnuplot_GifTest()
    img = BytesIO(gnuplot(commands,data))
    anImg = CoreImage(img,ext="png")

    def __init__(self,**kwargs):
        super(Plot,self).__init__(**kwargs)

        # HMM, how to display self.anImg??
        print self.anImg

class MyApp(App):

    def build(self):
        box = BoxLayout(orientation="vertical")
        box.add_widget(Plot())
        return box

MyApp().run()

【问题讨论】:

标签: python kivy gnuplot


【解决方案1】:

经过一番研究,我找到了解决问题的方法。如果有其他解决方案,我会在周末向其他读者开放。

from subprocess import Popen, PIPE
from StringIO import StringIO                                            
from io import BytesIO
from os import linesep as nl
from PIL import Image as Image

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.core.image import Image as CoreImage
from kivy.uix.image import Image as UIXImage

def gnuplot(commands, data):                                                    
    """ drive gnuplot, expects lists, returns stdout as string """              

    dfile = StringIO()                                                          
    for line in data:                                                           
        dfile.write(str(line) + nl)                                             

    args = ["gnuplot", "-e", (";".join([str(c) for c in commands]))]            
    p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)                       

    dfile.seek(0)                                                               
    return p.communicate(dfile.read())[0]   


def gnuplot_GifTest():
    commands = [\
        "set datafile separator ','",\
        "set terminal gif",\
        "set output",\
        "plot '-' using 1:2 with linespoints, '' using 1:2 with linespoints",\
        ]
    data = [\
        "1,1",\
        "2,2",\
        "3,5",\
        "4,2",\
        "5,1",\
        "e",\
        "1,5",\
        "2,4",\
        "3,1",\
        "4,4",\
        "5,5",\
        "e",\
        ]

    return (commands, data)


class Plot(UIXImage):

    def __init__(self,commands,data,**kwargs):
        super(Plot,self).__init__(**kwargs)
        gnu_img = BytesIO(gnuplot(commands,data))
        gnu_converted = CoreImage(gnu_img,ext="gif")
        self.texture = gnu_converted.texture


class MyApp(App):

    def build(self):
        (commands, data) = gnuplot_GifTest() # example for passing gnuplot commands and data to the plot widget
        box = BoxLayout(orientation="vertical")
        box.add_widget(Plot(commands,data))
        return box

MyApp().run()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多