Skip to content

Instantly share code, notes, and snippets.

@GreyGnome
Created June 27, 2016 18:26
Show Gist options
  • Save GreyGnome/b0f94ae5cdb27a4f03c6ef307b3d551a to your computer and use it in GitHub Desktop.
Save GreyGnome/b0f94ae5cdb27a4f03c6ef307b3d551a to your computer and use it in GitHub Desktop.
Example py file. If I remove the Color line, the MyLabel will inherit the color of its enclosing Layout.
import kivy
kivy.require('1.0.8')
from kivy.app import App
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Rectangle
from kivy.properties import StringProperty
from kivy.uix.image import Image
from kivy.graphics import Color
Builder.load_file('scrollview.kv')
class MyFloatLayout(FloatLayout):
def __init__(self, name, *args, **kwargs):
super(MyFloatLayout, self).__init__(*args, **kwargs)
self.name = name
class MyLabel(Label):
image = StringProperty()
def __init__(self, *args, **kwargs):
super(MyLabel, self).__init__(*args, **kwargs)
with self.canvas.before:
# If I delete the following line, this label inherits the Color value
# of its containing Layout:
Color(1.0, 1.0, 1.0)
Rectangle(source=self.image, pos=self.pos, size=self.size)
# listen to size and position changes
self.bind(pos=self.update_rect, size=self.update_rect)
def update_rect(instance, value, new_position):
# value is a MyLabel object
instance.rect.pos = instance.pos
instance.rect.size = instance.size
class TestApp(App):
def build(self):
root = MyFloatLayout("root")
odd_label = MyLabel(text="Oddball", image="disney.jpeg")
odd_image = Image(source="disney.jpeg", size=[400,300], pos=[200,200],
size_hint_x=None, size_hint_y=None)
root.add_widget(odd_label)
root.add_widget(odd_image)
return root
if __name__ == '__main__':
TestApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment