本文介绍了将文本渲染到奇异的画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试在猕猴桃的画布"中绘制自己的图形.现在,我有一个红色或绿色的矩形,该矩形每秒更改一次颜色,但是我想添加一个更改的文本标签.

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.

稍作搜索后,似乎没有可以添加到画布上的文本"指令.我发现了一些使用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.

这是我目前的目标:

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?

谢谢

推荐答案

回答我自己的问题:

环顾[kivy]花园后,我发现了Tickline(和Tick).以及CoreLabel()和Rectangle(texture = ...)的使用

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

这是我更新的render()方法,它添加了我需要的文本对象.

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!

这篇关于将文本渲染到奇异的画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:25