問(wèn)題描述
當(dāng)我嘗試將 id 分配給畫(huà)布時(shí),我收到錯(cuò)誤 Invalid data after declaration
,但我看不到任何其他引用該 id 的方式(例如 e1).如何在我的 Python 代碼中引用 e1?
I get the error Invalid data after declaration
when I try to assign an id to canvas, but I don't see any other way of referencing the id's further below (for example e1). How do I reference e1 in my Python code?
<MyClockWidget>:
face: face
ticks: ticks
el1: el1
FloatLayout:
id: face
size_hint: None, None
pos_hint: {"center_x":0.5, "center_y":0.5}
size: 0.9*min(root.size), 0.9*min(root.size)
canvas:
id: cand
Color:
rgb: 0.5, 0.5, 0.5
Ellipse:
size: self.size
pos: self.pos
canvas:
Color:
rgb: 0.1, 0.1, 0.1
Ellipse:
id: el1
size: self.size
pos: self.pos
angle_start: 0
angle_end: 90
Ellipse:
id: el2
size: self.size
pos: self.pos
angle_start: 110
angle_end: 130
推薦答案
我覺(jué)得用不到 instruction groups 有很好的文檔記錄,但這里有一個(gè)示例,說(shuō)明如何使用它們以后訪問(wèn) Canvas
元素.此示例還展示了如何使用屬性來(lái)控制 Canvas
指令的各個(gè)方面:
I don't think the use of instruction groups in kv lang is well documented, but here is an example for how to use them to later access Canvas
elements. This example also show how to use properties to control aspects of a Canvas
instruction:
from kivy.app import App
from kivy.uix.slider import Slider
from kivy.lang import Builder
from kivy.graphics import Color
kv = """
#:kivy 1.9.1
BoxLayout:
orientation: 'vertical'
Widget:
id: w_canvas
my_color: (0, 1, 1)
canvas:
Color:
rgb: self.my_color
Rectangle:
pos: self.pos
size: (self.width/2, self.height/2)
Color:
group: 'b'
rgb: (0, .8, 0)
Ellipse:
group: 'a'
pos: (self.pos[0], self.pos[1] + self.height/2)
size: (self.width/4, self.height/4)
Ellipse:
group: 'b'
pos: (self.pos[0]+ self.width/2, self.pos[1] + self.height/2)
size: (self.width/4, self.height/4)
Button:
text: 'Click me'
on_release: app.handle_button()
"""
class Test(App):
def build(self):
return Builder.load_string(kv)
def handle_button(self):
# binding Canvas instruction property to Widget property
self.root.ids.w_canvas.my_color = (.5, .2, 0)
# Access single item of canvas instruction group
an_ellipse = self.root.ids.w_canvas.canvas.get_group('a')[0]
an_ellipse.pos = (an_ellipse.pos[0] + 10, an_ellipse.pos[1])
# loop through all elements of canvas instruction group
for gitem in self.root.ids.w_canvas.canvas.get_group('b'):
if isinstance(gitem, Color):
gitem.rgb = (0, .5, 1)
try:
gitem.size = (gitem.size[0] / 2.0, gitem.size[1])
except:
pass
Test().run()
這篇關(guān)于如何在 Kivy 的畫(huà)布中引用孩子的 id?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!