Skip to content

Instantly share code, notes, and snippets.

@velolala
Created February 11, 2015 22:15
Show Gist options
  • Save velolala/6b33fc3333313f7d08f9 to your computer and use it in GitHub Desktop.
Save velolala/6b33fc3333313f7d08f9 to your computer and use it in GitHub Desktop.
Painting __init__ vs .kv init
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.scatterlayout import ScatterLayout
from kivy.uix.widget import Widget
from kivy.graphics import Line
kv = """
<Test>:
orientation: 'vertical'
width: 320
height: 480
MyWidget
x: 20
y: 20
size_hint: (.9, .9)
MyWidget
pos_hint: dict(x=.5, y=.5)
size_hint: (20 / self.parent.width, 20 / self.parent.height)
"""
Builder.load_string(kv)
thisworks = """
<MyWidget@Widget>:
text: 'My Widget'
canvas:
Line:
rectangle: self.x, self.y, self.width, self.height
"""
# Uncomment next line, and it works as expected:
#Builder.load_string(thisworks)
class MyWidget(Widget):
"""This definition of MyWidget does not work as expected.
If I was using
Builder.load_string(thisworks)
it would work as expected.
"""
def __init__(self, **kwargs):
super(MyWidget, self).__init__(**kwargs)
with self.canvas.after:
Line(rectangle=[self.x, self.y, self.width, self.height])
class Test(ScatterLayout):
def __init__(self, *args, **kwargs):
super(Test, self).__init__(*args, **kwargs)
class TestApp(App):
def build(self):
return Test()
if __name__ == '__main__':
TestApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment