Created
June 23, 2016 12:46
-
-
Save GreyGnome/1659a26dcfdb1507298c860cbe8ace2b to your computer and use it in GitHub Desktop.
My scrollview testing app. Notice how the first image in any container has distorted colors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kivy | |
kivy.require('1.0.8') | |
from kivy.app import App | |
from kivy.uix.scrollview import ScrollView | |
from kivy.uix.stacklayout import StackLayout | |
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 | |
import os | |
Builder.load_file('scrollview.kv') | |
class MyFloatLayout(FloatLayout): | |
def __init__(self, name, *args, **kwargs): | |
super(MyFloatLayout, self).__init__(*args, **kwargs) | |
self.name = name | |
# TESTPOINT FLOAT | |
# def on_touch_down(self, touch): | |
# super(MyFloatLayout, self).on_touch_down(touch) | |
# print "MyFloatLayout: touched!", self.name | |
class MyLabel(Label): | |
def __init__(self, *args, **kwargs): | |
super(MyLabel, self).__init__(*args, **kwargs) | |
# It doesn't matter if I uncomment this or not. | |
# image = StringProperty("IMG_20140125_132125.jpg-thmb.jpg") | |
with self.canvas.before: | |
self.rect = Rectangle(source='IMG_20140125_132125.jpg-thmb.jpg', 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 | |
# TESTPOINT LABEL | |
# def on_touch_down(self,touch): | |
# print "MyLabel: touched!", self.text | |
class MyScrollView(ScrollView): | |
def __init__(self, name, *args, **kwargs): | |
super(MyScrollView, self).__init__(*args, **kwargs) | |
self.name = name | |
# TESTPOINT SCROLL | |
# def on_touch_down(self, touch): | |
# super(MyScrollView, self).on_touch_down(touch) | |
# print "MyScrollView: touched!", self.name | |
# return True | |
class MyStackLayout(StackLayout): | |
def __init__(self, name, *args, **kwargs): | |
super(MyStackLayout, self).__init__(*args, **kwargs) | |
self.name = name | |
# TESTPOINT STACK | |
# def on_touch_down(self, touch): | |
# super(MyStackLayout, self).on_touch_down(touch) | |
# print "MyStackLayout: touched!", self.name | |
class ScrollViewApp(App): | |
def build(self): | |
root = MyFloatLayout("root") | |
# for j in [[1,1], [2,1], [1,2], [2,2]]: | |
for j in [[1,1]]: | |
# create a default grid layout with custom width/height. Find the specs in the .kv file | |
mystacklayout = MyStackLayout(str(j)) | |
# when we add children to the grid layout, its size doesn't change at | |
# all. we need to ensure that the height will be the minimum required to | |
# contain all the childs. (otherwise, we'll child outside the bounding | |
# box of the view) | |
mystacklayout.bind(minimum_height=mystacklayout.setter('height')) | |
label_group = str(j) | |
# add button into that grid | |
for i in range(5): | |
a_label = MyLabel(text=label_group + "_lbl: " + str(i)) | |
mystacklayout.add_widget(a_label) | |
# create a scroll view, with a size < size of the grid | |
x = float(j[0])**2 * 80 | |
y = float(j[1])**2 * 80 | |
myscrollview = MyScrollView(str(j), pos=(x,y)) # Specs also in the .kv | |
myscrollview.add_widget(mystacklayout) | |
root.add_widget(myscrollview) | |
odd_label = MyLabel(text="Oddball") | |
root.add_widget(odd_label) | |
return root | |
if __name__ == '__main__': | |
ScrollViewApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment