Rendering text to a kivy canvas

情到浓时终转凉″ 提交于 2021-02-08 14:17:29

问题


I am trying to draw my own graphic within a kivy 'canvas'. For now I have a red or green rectangle which changes colour once per second, but I want to add a changing text label.

After a little searching it appears that there isn't a "Text" Instruction which can be added to the canvas. I have found a few references to using a Label() widget as well as the canvas Instructions, but this does not seem ideal, and also I can't seem to get it to render more than once.

Here's my object as it stands at the moment:

class HVObject(BoxLayout):
    def __init__(self, **kwargs):
        BoxLayout.__init__(self, **kwargs)
        self.colour = 1
        self.label = Label()
        self.render()
        self.add_widget(self.label)

        self.bind(size=self._update_rect, pos=self._update_rect)
        Clock.schedule_interval(self.callevery, 1)

    def render(self):
        self.canvas.clear()
        self.rect = Rectangle(size=self.size, pos=self.pos)
        self.canvas.add(Color(1-self.colour, self.colour, 0, 1))
        self.canvas.add(self.rect)
        self.label.text = "COL %d" % self.colour
        self.canvas.ask_update()

    def callevery(self, x):
        self.colour = 1-self.colour
        self.render()

    def _update_rect(self, instance, value):
        self.rect.pos = instance.pos
        self.rect.size = instance.size
        self.label.pos = instance.pos

Is there an easy way to achieve the effect I need?

Thank you


回答1:


Answering my own question:

After a little look around the [kivy] garden, I found Tickline (and Tick). and the use of CoreLabel() and Rectangle(texture=...)

Here's my updated render() method which adds the text object I need.

    def render(self):
        self.canvas.clear()
        self.canvas.add(Color(1-self.colour, self.colour, 0, 1))
        self.rect = Rectangle(size=self.size, pos=self.pos)
        self.canvas.add(self.rect)
        label = CoreLabel(text="COL %d" % self.colour, font_size=20)
        label.refresh()
        text = label.texture
        self.canvas.add(Color(self.colour, 1-self.colour,0, 1))
        pos = list(self.pos[i] + (self.size[i] - text.size[i]) / 2 for i in range(2))
        self.canvas.add(Rectangle(size=text.size, pos=pos, texture=text))
        self.canvas.ask_update()

Which works for me, albeit a little clunky!



来源:https://stackoverflow.com/questions/33489523/rendering-text-to-a-kivy-canvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!