【问题标题】:Kivy garden graph opacity animationKivy 花园图不透明度动画
【发布时间】:2021-06-08 20:43:32
【问题描述】:

我正在尝试使用 kivy 中的动画工具使 kivy_garden Graph 的 MeshLinePlot 淡入。据我了解,使线条透明的唯一方法是将 MeshLinePlot 的颜色属性中的 alpha 参数设置为 0。它有效,但仅适用于线条的一小部分。我需要使整条线透明。我在互联网上找不到任何可以帮助我的东西。代码如下:

from kivy.app import App
import numpy as np
from math import sin, sqrt, exp
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy_garden.graph import Graph, MeshLinePlot, SmoothLinePlot
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.animation import Animation


class myApp(App):
    lbl=Label(text="0", size_hint_y=0.3)

    S0=50
    mu=0.05
    sig=0.3
    dt=1/365



    graph=Graph(
                    xmin=0, 
                    xmax=50, 
                    ymin=0, 
                    ymax=100, 
                    x_ticks_major=50,
                    y_ticks_major=10,
                    tick_color=[0.06,0.06,0.06,1],
                    draw_border=False,
                    x_grid=True,
                    y_grid=True
    )



    plot2 = MeshLinePlot(color=[1, 0, 0, 1])
    first_point=(0,50) 
    plot2.points=[first_point]
    graph.add_plot(plot2)
    event=None
    St=S0

    def on_start(self):
        self.event=Clock.schedule_interval(self.update_plot, 1/60)

    def build(self):
        bl=BoxLayout(orientation="vertical")
        bl.add_widget(self.lbl)
        bl.add_widget(self.graph)
        return bl

    def update_plot(self, *ARGS):
        drift=(self.mu-self.sig*self.sig*0.5)*self.dt
        epsilon=np.random.normal()
        var=epsilon*self.sig*sqrt(self.dt)
        self.St=self.St*exp(drift+var)
        last_x2=self.plot2.points[len(self.plot2.points)-1][0]
        new_point2=(last_x2+1, self.St)
        self.plot2.points.append(new_point2)
        self.lbl.text=str(last_x2)
        if last_x2+1==self.graph.xmax:
            Clock.unschedule(self.event)
            self.lbl.text=str("Clocking stopped")
            anim=Animation(color=[1, 0, 0, 0.0])  #<-- problem here
            anim.start(self.plot2)
        

myApp().run()

【问题讨论】:

    标签: python-3.x kivy


    【解决方案1】:

    您可以通过将其opacity 设置为0 来使整个图形透明:

    graph.opacity = 0
    

    然后在您的 update_plot() 方法中,您可以为该不透明度设置动画:

            anim = Animation(opacity=1, duration=5)  # <-- problem here
            anim.start(self.graph)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-25
      • 1970-01-01
      • 2011-08-27
      • 2012-09-09
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多