【问题标题】:How to change the background color of a kivy Graph?如何更改 kivy Graph 的背景颜色?
【发布时间】:2018-06-21 12:25:19
【问题描述】:

是否存在类似于下面的属性来更改 Kivy 中 Graph 的背景颜色?

test.kv

Graph:
    id: my_graph_id
    border_color = (0,0,0,0) # this is valid
    background_color = (0,0,0,0) # ?? this does not exist

【问题讨论】:

  • 在 kv 文件中,将 = 替换为 : 例如background_color: (0, 0, 1, 1) # blue

标签: python kivy kivy-language


【解决方案1】:

在 Python 代码中使用 graph.background_color。如下所示,Kivy Garden Graph 确实存在 background_color 属性。

示例

main.py

​​>
from math import sin
from kivy.garden.graph import Graph, MeshLinePlot
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout


class RootWidget(BoxLayout):

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

        graph = Graph(xlabel='X', ylabel='Y', x_ticks_minor=5, x_ticks_major=25, y_ticks_major=1,
                      y_grid_label=True, x_grid_label=True, padding=5,
                      x_grid=True, y_grid=True, xmin=-0, xmax=100, ymin=-1, ymax=1)

        graph.background_color = 0, 0, 1, 1    # blue color

        plot = MeshLinePlot(color=[1, 0, 0, 1])
        plot.points = [(x, sin(x / 10.)) for x in range(0, 101)]
        graph.add_plot(plot)
        self.add_widget(graph)


class GraphDemo(App):
    title = "Kivy Garden Graph - background_color"

    def build(self):
        return RootWidget()


if __name__ == "__main__":
    GraphDemo().run()

输出

【讨论】:

    猜你喜欢
    • 2015-08-14
    • 1970-01-01
    • 2023-03-19
    • 2014-01-19
    • 1970-01-01
    • 2017-07-25
    • 2013-12-09
    • 1970-01-01
    相关资源
    最近更新 更多