如何在Kivy中将画布分配给多个小部件

时间:2019-01-21 21:40:53

标签: python canvas kivy

我想为屏幕上的所有小部件提供白色画布。我知道如何在KV中创建预期的画布,但是在这种情况下,我必须使用Python代码。

在我的代码中,我尝试了self.cavas.add(...),在下面的代码中,我使用了with self.canvas:。两次尝试都导致在屏幕的角落而不是在小部件内部绘制画布。

如何使用Python代码在每个小部件内放置一个画布?

代码

from kivy.app import App
from kivy.core.window import Window
from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions import Rectangle
from kivy.lang import Builder
from random import random
from kivy.uix.button import Button
from kivy.animation import Animation
from kivy.clock import Clock

class LittleButtons(Button):

    dur = 2

    def reup(self, *args):

        Animation.cancel_all(self)
        Animation(pos_hint = {'center_x': random(), 'center_y': random()}, duration = self.dur).start(self)

    def __init__(self, **kwargs):

        super(LittleButtons, self).__init__(**kwargs)
        self.pos_hint = {'center_x': random(), 'center_y': random()}
        self.size_hint = None, None
        self.width = random() * (Window.width / 20)
        self.height = self.width
        self.background_color = [0,0,0,.05]
        with self.canvas:
            Color(rgba = [1,1,1,.2])
            Rectangle(pos = self.pos, size = self.size)

        Animation(pos_hint = {'center_x': random(), 'center_y': random()}, duration = self.dur).start(self)
        Clock.schedule_interval(self.reup, self.dur)

KV = Builder.load_string("""
#:import Factory kivy.factory.Factory

Screen:
    FloatLayout:
        on_parent:
            (lambda ltext: [self.add_widget(Factory.LittleButtons(text=ltext)) for i in range (150)])('hi!')
        Button:
            background_color: 0,0,0,0
            canvas:
                Color:
                    rgba: 0,1,1,1
                Rectangle:
                    pos: self.pos
                    size:self.size
""")

class MyApp(App):
    def build(self):
        return KV

if __name__ == '__main__':
    MyApp().run()

1 个答案:

答案 0 :(得分:1)

问题在于,在LittleButtons __init__()方法中,其位置仍然是默认值(0,0),因此Rectangle的位置设置为(0, 0)。当您使用KV时,当您引用Rectangle时,它将LittleButtons位置绑定到self.pos位置。不幸的是,在.py文件中,您必须自己提供该绑定。因此,这是您LittleButtons的修改,可以处理位置变化:

class LittleButtons(Button):

    dur = 2

    def reup(self, *args):

        Animation.cancel_all(self)
        Animation(pos_hint = {'center_x': random(), 'center_y': random()}, duration = self.dur).start(self)

    def __init__(self, **kwargs):
        self.rect = None
        super(LittleButtons, self).__init__(**kwargs)
        self.pos_hint = {'center_x': random(), 'center_y': random()}
        self.size_hint = None, None
        self.width = random() * (Window.width / 20)
        self.height = self.width
        self.background_color = [0,0,0,.05]
        with self.canvas:
            Color(rgba = [1,1,1,.2])
            self.rect = Rectangle(pos = self.pos, size = self.size)

        Animation(pos_hint = {'center_x': random(), 'center_y': random()}, duration = self.dur).start(self)
        Clock.schedule_interval(self.reup, self.dur)


    def on_pos(self, instance, pos):
        if self.rect is not None:
            self.rect.pos = pos

所做的更改是添加了self.rect属性和on_pos方法。

如果需要处理更改size,可以添加类似内容。