Advertisement
FoxboyPrower

KivyChildSizeHelp

Aug 1st, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 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. #GreyLabel will be dynamically added to MyFrame inside the first ResizingRow
  12. Builder.load_string('''
  13. <GreyLabel>:
  14.    canvas.before:
  15.        Color:
  16.            rgba: .7, .7, .7, 1
  17.        Rectangle:
  18.            pos: self.pos
  19.            size: self.size
  20.  
  21. <Resizing_GridLayout@GridLayout>:
  22.    cols: 1
  23.    row_force_default: True
  24.    #foo: [self.rows_minimum.update({i: x.height}) for i, x in enumerate(reversed(list(self.children)))]
  25. <ResizingRow_GridLayout@GridLayout>:
  26.    cols: 1
  27.    height: sum([c.height for c in self.children])
  28.  
  29. <ContainerBox>:
  30.    orientation: 'horizontal'
  31.  
  32.    Resizing_GridLayout:
  33.        ResizingRow_GridLayout:
  34.            id: Row1
  35.            MyFrame:
  36.                id: TitleContent
  37.        ResizingRow_GridLayout:
  38.            id: Row2
  39.            Label:
  40.                height: 30
  41.                text: 'Label One'
  42.            TextInput:
  43.                height: 30
  44.                multiline: False
  45.                write_tab: False
  46.                hint_text: 'Insert one liner'
  47.  
  48.        ResizingRow_GridLayout:
  49.            id: Row3
  50.            Label:
  51.                height: 45
  52.                text: 'Label two'
  53.            Button:
  54.                text: 'Button One'
  55.                height: 60
  56.            GridLayout:
  57.                rows: 1
  58.                height: 25
  59.                Button:
  60.                    text: 'Button Two'
  61.                Button:
  62.                    text: 'Button three'
  63. ''')
  64. class GreyLabel(Label):
  65.     def __init__(self, **kwargs):
  66.         super(GreyLabel, self).__init__(**kwargs)
  67.  
  68. class Resizing_GridLayout(GridLayout):
  69.     def __init__(self, **kwargs):
  70.         super(Resizing_GridLayout, self).__init__(**kwargs)
  71.         self.foo = [self.rows_minimum.update({i: x.height}) for i, x in enumerate(reversed(list(self.children)))]
  72.  
  73. class ResizingRow_GridLayout(GridLayout):
  74.     def __init__(self, **kwargs):
  75.         super(ResizingRow_GridLayout, self).__init__(**kwargs)
  76.  
  77.  
  78. class MyFrame(Widget):
  79.     c_value = StringProperty('SomeThing goes here')
  80.  
  81.     def __init__(self, **kwargs):
  82.         super(MyFrame, self).__init__(**kwargs)
  83.         Clock.schedule_once(lambda dt: self.makeLabel(), timeout=0.1)
  84.  
  85.     def makeLabel(self):
  86.         c_label = GreyLabel()
  87.         self.bind(pos=c_label.setter('pos'), width=c_label.setter('width'), c_value=c_label.setter('text'))
  88.         self.add_widget(c_label)
  89.         # this forces a property event so the label's text will be changed
  90.         Clock.schedule_once(lambda dt: self.property('c_value').dispatch(self), 0.5)
  91.         # this forces a property event so the label's pos will be changed
  92.         Clock.schedule_once(lambda dt: self.chg_text(c_label), 1)
  93.  
  94.     def chg_text(self, p_widget):
  95.         # this forces a property event so the label's text will be changed
  96.         self.property('c_value').dispatch(self)
  97.         self.height = p_widget.height
  98.  
  99. class ContainerBox(BoxLayout):
  100.     def __init__(self, **kwargs):
  101.         super(ContainerBox, self).__init__(**kwargs)
  102.  
  103. class Nested2App(App):
  104.     def build(self):
  105.         return ContainerBox()
  106.  
  107.     def on_stop(self):
  108.         print()
  109.         print("Nested2App.on_start: starting")
  110.         f_Resizing_Grid = self.root.children[0]
  111.         f_Resizing_Row = self.root.children[0].children[2]
  112.         f_MyFrame = self.root.children[0].children[2].children[0]
  113.         f_FreshLabel = f_MyFrame.children[0]
  114.         print("\tf_Resizing_Grid size: \t", f_Resizing_Grid.size, "\t\theight:", f_Resizing_Grid.size[1])
  115.         print("\tf_Resizing_Grid pos : \t\t", f_Resizing_Grid.pos)
  116.         print("\tf_Resizing_Row size: \t", f_Resizing_Row.size, "\t\theight:", f_Resizing_Row.size[1])
  117.         print("\tf_Resizing_Row pos : \t\t", f_Resizing_Row.pos)
  118.         print("\tf_MyFrame size: \t\t", f_MyFrame.size, "\t\theight:", f_MyFrame.size[1])
  119.         print("\tf_MyFrame pos : \t\t\t", f_MyFrame.pos)
  120.         print("\tf_FreshLabel size: \t\t", f_FreshLabel.size, "\t\theight:", f_FreshLabel.size[1])
  121.         print("\tf_FreshLabel pos : \t\t\t", f_FreshLabel.pos)
  122.  
  123.  
  124. if __name__ == '__main__':
  125.     Nested2App().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement