我想为屏幕上的所有小部件提供白色 Canvas 。我知道如何在 KV
中创建预期的 Canvas ,但在本例中我必须使用 Python 代码。
在我的代码中,我尝试了 self.cavas.add(...)
,在下面的代码中,我使用 with self.canvas:
。这两次尝试都导致 Canvas 被绘制在屏幕的一角,而不是小部件的内部。
如何使用 Python 代码将 Canvas 放入每个小部件内?
代码:
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()
最佳答案
问题在于,在LittleButtons
__init__()
方法中,它的位置仍然是默认的(0,0),所以的位置矩形
设置为 (0,0)。当您使用KV
时,当您引用self.pos
时,它会巧妙地将Rectangle
位置绑定(bind)到LittleButtons
位置。不幸的是,在 .py
文件中,您必须自己提供该绑定(bind)。因此,这里是对 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
方法。
如果您需要处理更改大小
,您可以添加类似的内容。
关于python - 如何在 Kivy 中将 Canvas 分配给多个小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54298128/