Advertisement
FoxboyPrower

KivyStrechHelp

Aug 1st, 2019
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.64 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.lang import Builder
  3. from kivy.uix.widget import Widget
  4. from kivy.uix.label import Label
  5. from kivy.clock import Clock
  6. from kivy.uix.boxlayout import BoxLayout
  7. from kivy.uix.gridlayout import GridLayout
  8. from kivy.properties import StringProperty
  9. from kivy.uix.textinput import TextInput
  10. from kivy.properties import BooleanProperty, ObjectProperty
  11. #This now has StretchingLabel, which will change size to match it's dynamically changing contents
  12. Builder.load_string('''
  13. <StretchingLabel>:
  14.    padding: 10, 5
  15.    size_hint_y: None
  16.    text_size: self.width, None
  17.    height: self.texture_size[1]
  18.    group: 'test'
  19.    canvas.before:
  20.        Color:
  21.            rgba: .7, .7, .7, 1
  22.        Rectangle:
  23.            pos: self.pos
  24.            size: self.size
  25.  
  26. <Resizing_GridLayout@GridLayout>:
  27.    cols: 1
  28.    row_force_default: True
  29.    #foo: [self.rows_minimum.update({i: x.height}) for i, x in enumerate(reversed(list(self.children)))]
  30. <ResizingRow_GridLayout@GridLayout>:
  31.    cols: 1
  32.    height: sum([c.height for c in self.children])
  33.  
  34. <ContainerBox>:
  35.    orientation: 'horizontal'
  36.  
  37.    Resizing_GridLayout:
  38.        ResizingRow_GridLayout:
  39.            id: Row1
  40.            MyFrame:
  41.                id: TitleContent
  42.        ResizingRow_GridLayout:
  43.            id: Row2
  44.            Label:
  45.                height: 30
  46.                text: 'Label One'
  47.            TextInput:
  48.                height: 30
  49.                multiline: False
  50.                write_tab: False
  51.                hint_text: 'Insert one liner'
  52.  
  53.        ResizingRow_GridLayout:
  54.            id: Row3
  55.            Label:
  56.                height: 45
  57.                text: 'Label two'
  58.            Button:
  59.                text: 'Button One'
  60.                height: 60
  61.            GridLayout:
  62.                rows: 1
  63.                height: 25
  64.                Button:
  65.                    text: 'Button Two'
  66.                Button:
  67.                    text: 'Button three'
  68. ''')
  69.  
  70. class StretchingLabel(Label):
  71.     edit = BooleanProperty(False)
  72.     textinput = ObjectProperty(None, allownone=True)
  73.  
  74.     def on_touch_down(self, touch):
  75.         if self.collide_point(*touch.pos) and touch.is_double_tap and not self.edit:
  76.             self.edit = True
  77.         return super(StretchingLabel, self).on_touch_down(touch)
  78.  
  79.     def on_edit(self, instance, value):
  80.         if not value:
  81.             if self.textinput:
  82.                 self.remove_widget(self.textinput)
  83.             return
  84.         self.textinput = t = TextInput(
  85.             text=self.text, size_hint=(None, None),
  86.             font_size=self.font_size, font_name=self.font_name,
  87.             pos=self.pos, size=self.size, multiline=False)
  88.         self.bind(pos=t.setter('pos'), size=t.setter('size'))
  89.         self.add_widget(self.textinput)
  90.         t.bind(on_text_validate=self.on_text_validate, focus=self.on_text_focus)
  91.  
  92.     def on_text_validate(self, instance):
  93.         self.text = instance.text
  94.         self.edit = False
  95.  
  96.     def on_text_focus(self, instance, focus):
  97.         if focus is False:
  98.             self.text = instance.text
  99.             self.edit = False
  100.  
  101. class Resizing_GridLayout(GridLayout):
  102.     def __init__(self, **kwargs):
  103.         super(Resizing_GridLayout, self).__init__(**kwargs)
  104.         self.foo = [self.rows_minimum.update({i: x.height}) for i, x in enumerate(reversed(list(self.children)))]
  105.  
  106. class ResizingRow_GridLayout(GridLayout):
  107.     def __init__(self, **kwargs):
  108.         super(ResizingRow_GridLayout, self).__init__(**kwargs)
  109.  
  110.  
  111. class MyFrame(Widget):
  112.     c_value = StringProperty('SomeThing goes here')
  113.  
  114.     def __init__(self, **kwargs):
  115.         super(MyFrame, self).__init__(**kwargs)
  116.         Clock.schedule_once(lambda dt: self.makeLabel(), timeout=0.1)
  117.  
  118.     def makeLabel(self):
  119.         c_label = StretchingLabel()
  120.         self.bind(pos=c_label.setter('pos'), width=c_label.setter('width'), c_value=c_label.setter('text'))
  121.         self.add_widget(c_label)
  122.         # this forces a property event so the label's text will be changed
  123.         Clock.schedule_once(lambda dt: self.property('c_value').dispatch(self), 0.5)
  124.         # this forces a property event so the label's pos will be changed
  125.         Clock.schedule_once(lambda dt: self.chg_text(c_label), 1)
  126.  
  127.     def chg_text(self, p_widget):
  128.         # this forces a property event so the label's text will be changed
  129.         self.property('c_value').dispatch(self)
  130.         self.height = p_widget.height
  131.  
  132. class ContainerBox(BoxLayout):
  133.     def __init__(self, **kwargs):
  134.         super(ContainerBox, self).__init__(**kwargs)
  135.  
  136. class Nested2App(App):
  137.     def build(self):
  138.         return ContainerBox()
  139.  
  140.     def on_stop(self):
  141.         print()
  142.         print("Nested2App.on_start: starting")
  143.         f_Resizing_Grid = self.root.children[0]
  144.         f_Resizing_Row = self.root.children[0].children[2]
  145.         f_MyFrame = self.root.children[0].children[2].children[0]
  146.         f_FreshLabel = f_MyFrame.children[0]
  147.         print("\tf_Resizing_Grid size: \t", f_Resizing_Grid.size, "\t\theight:", f_Resizing_Grid.size[1])
  148.         print("\tf_Resizing_Grid pos : \t\t", f_Resizing_Grid.pos)
  149.         print("\tf_Resizing_Row size: \t", f_Resizing_Row.size, "\t\theight:", f_Resizing_Row.size[1])
  150.         print("\tf_Resizing_Row pos : \t\t", f_Resizing_Row.pos)
  151.         print("\tf_MyFrame size: \t\t", f_MyFrame.size, "\t\theight:", f_MyFrame.size[1])
  152.         print("\tf_MyFrame pos : \t\t\t", f_MyFrame.pos)
  153.         print("\tf_FreshLabel size: \t\t", f_FreshLabel.size, "\t\theight:", f_FreshLabel.size[1])
  154.         print("\tf_FreshLabel pos : \t\t\t", f_FreshLabel.pos)
  155.  
  156.  
  157. if __name__ == '__main__':
  158.     Nested2App().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement