I am trying to reference to Canvas within GridLayout with just drawing simple rectangle before further development. Code:
main.py:
JavaScript
x
23
23
1
import kivy
2
from kivy.app import App
3
from kivy.uix.widget import Widget
4
from kivy.lang import Builder
5
from kivy.graphics.vertex_instructions import Rectangle
6
from kivy.graphics.context_instructions import Color
7
8
class Grafika(Widget):
9
def __init__(self, **kwargs):
10
super(Grafika,self).__init__(**kwargs)
11
with self.canvas:
12
Color(1, 0, 0, 1)
13
Rectangle(pos=self.pos,size=self.size)
14
15
kv=Builder.load_file("my.kv")
16
17
class MyMainApp(App):
18
def build(self):
19
return kv
20
21
if __name__ == "__main__":
22
MyMainApp().run()
23
my.py
JavaScript
1
35
35
1
<TextInput>:
2
font_size:20
3
color: 0.3, 0.6,0.7,1
4
size_hint: (0.3,0.5)
5
<Button>:
6
font_size:20
7
color: 0.3, 0.6,0.7,1
8
size_hint: (0.3,0.3)
9
10
11
Grafika:
12
GridLayout:
13
cols:2
14
GridLayout:
15
size_hint: (0.3,0.2)
16
cols:1
17
GridLayout:
18
cols:2
19
Label:
20
text:"no"
21
TextInput:
22
text: "50"
23
Label:
24
text:"rbr"
25
TextInput:
26
text: "100"
27
Button:
28
text:"calc"
29
canvas:
30
Color:
31
rbg:1, 1, 1, 1
32
Rectangle:
33
pos:self.pos
34
size:self.size
35
After running i get error:
JavaScript
1
9
1
2
27: Button:
3
28: text:"calc"
4
>> 29: canvas:
5
30: Color:
6
31: rbg:1, 1, 1, 1
7
8
Canvas instructions added in kv must be declared before child widgets.
9
I am new in kivy, so any help is appreciated.
After solving this issue, I am planning to run custom python func , run under button calc, and return calculated results on canvas. Func results are 2d line points, hence plotting of results on canvas is goal here.
Advertisement
Answer
Change:
JavaScript
1
25
25
1
Grafika:
2
GridLayout:
3
cols:2
4
GridLayout:
5
size_hint: (0.3,0.2)
6
cols:1
7
GridLayout:
8
cols:2
9
Label:
10
text:"no"
11
TextInput:
12
text: "50"
13
Label:
14
text:"rbr"
15
TextInput:
16
text: "100"
17
Button:
18
text:"calc"
19
canvas:
20
Color:
21
rbg:1, 1, 1, 1
22
Rectangle:
23
pos:self.pos
24
size:self.size
25
to:
JavaScript
1
25
25
1
Grafika:
2
GridLayout:
3
cols:2
4
canvas:
5
Color:
6
rbg:1, 1, 1, 1
7
Rectangle:
8
pos:self.pos
9
size:self.size
10
GridLayout:
11
size_hint: (0.3,0.2)
12
cols:1
13
GridLayout:
14
cols:2
15
Label:
16
text:"no"
17
TextInput:
18
text: "50"
19
Label:
20
text:"rbr"
21
TextInput:
22
text: "100"
23
Button:
24
text:"calc"
25
to put the canvas:
instruction before the children.