Advertisement
gr4viton

stacklayout auto height

Jun 27th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.69 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.lang import Builder
  3. from kivy.app import App
  4. from kivy.uix.gridlayout import GridLayout
  5. from kivy.uix.textinput import TextInput
  6. from kivy.uix.button import Button
  7.  
  8.  
  9. from kivy.uix.togglebutton import ToggleButton
  10. from kivy.properties import ObjectProperty, StringProperty, NumericProperty
  11. from kivy.uix.behaviors import ButtonBehavior
  12. from kivy.config import Config
  13.  
  14.  
  15. from kivy.graphics import Color, Rectangle
  16. from kivy.uix.floatlayout import FloatLayout
  17. from kivy.uix.stacklayout import StackLayout
  18. from kivy.uix.boxlayout import BoxLayout
  19. from kivy.uix.anchorlayout import AnchorLayout
  20.  
  21. from kivy.uix.scrollview import ScrollView
  22.  
  23. txt='''
  24. #:kivy 1.9
  25.  
  26. <steps_demo>:
  27.  
  28.    layout_steps: layout_steps
  29.    cols: 1
  30.    GridLayout: # right tab
  31.        cols: 1
  32.        padding: 5
  33.        GridLayout: # step window layout
  34.            cols:1
  35.            id: grid_steps
  36.            ScrollView: # step scrolling
  37.                id: scroll_steps
  38.  
  39.                min_move: 5
  40.                scroll_distance: min(self.width, self.height) * 0.8
  41.                do_scroll_x: 0
  42.  
  43.                StackLayout:
  44.                    height: self.minimum_height - 0
  45.                    size_hint_y: None
  46.                    id: layout_steps
  47.                    orientation: 'lr-tb'
  48.  
  49.  
  50. #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  51. <StepWidgetInfo>
  52.    multiline: 'True'
  53.    text: '2\\n3'
  54.    size_hint: 1, None
  55.    height: (len(self._lines)+1) * self.line_height
  56.    on_text: self.update_parent()
  57. #    on_enter: self.update_parent()
  58.    on_focus: self.update_parent()
  59.  
  60. #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  61. <StepWidget>:
  62.  
  63.    bottom_layout: bottom_layout
  64.  
  65. #    orientation: 'vertical'
  66.    cols:1
  67.  
  68.    size_hint_y: None
  69.    size_hint_x: None
  70.  
  71.    width: 225
  72. #    height: sum([child.height for child in self.children])
  73. #    height: self.minimum_height
  74.    cols: 1
  75.  
  76.    padding: (4,4,4,4)
  77.    spacing: (4,4)
  78.    margin: 1
  79.    color: .08,.16 , .24
  80.    canvas.before:
  81.        Color:
  82.            rgb: self.color
  83.        Rectangle:
  84.            pos: self.x + self.margin, self.y + self.margin + 1
  85.            size: self.width - 2 * self.margin , root.height - 2 * self.margin
  86.  
  87.    id: bottom_layout
  88.  
  89.    GridLayout:
  90.        cols: 2
  91.        size_hint_y: None
  92.        height: 0.15 * 250
  93.  
  94.        ToggleButton:
  95.            id: tbt_narrow
  96.            text: root.name
  97.            size_hint_x: 0.9
  98.            state: 'down'
  99. #            on_state: root.set_narrowed(narrowed=(self.state != 'down'), from_gui=True)
  100.  
  101.            on_state: root.clear_widgets()
  102.  
  103.  
  104. '''
  105.  
  106. Builder.load_string(txt)
  107.  
  108.  
  109. # class StepWidgetInfo(ScrollView):
  110. class StepWidgetInfo(TextInput):
  111.     update_height = None
  112.     last_lines = 0
  113.     def update_parent(self):
  114.         if len(self._lines) != self.last_lines:
  115.             self.last_lines = len(self._lines)
  116.             self.height = (len(self._lines)+1) * self.line_height
  117.             self.update_height()
  118.  
  119.  
  120.  
  121. # class StepWidget(BoxLayout):
  122. # class StepWidget(StackLayout):
  123. # class StepWidget(AnchorLayout):
  124. class StepWidget(GridLayout):
  125.     name = 'step'
  126.     bottom_layout = ObjectProperty()
  127.     def update_height(self):
  128.         heights = [child.height for child in self.children]
  129.         spacing = len(heights) - 1
  130.         self.height = sum(heights) + (spacing * self.spacing[0]) + self.padding[0] + self.padding[2]
  131.         print('updating')
  132.  
  133.     def __init__(self, **kwargs):
  134.         super(StepWidget, self).__init__(**kwargs)
  135.         self.update_height()
  136.  
  137.     def clear_widgets(self, **kwargs):
  138.         super(StepWidget, self).clear_widgets(**kwargs)
  139.         self.add_widget(Button())
  140.         self.update_height()
  141.  
  142.     pass
  143.  
  144. class steps_demo(GridLayout):
  145.     layout_steps = ObjectProperty()
  146.     # txx = StringProperty()
  147.  
  148.     # def callback(self, whatever=None):
  149.     #     self.txx+='asdasd\n'
  150.     # def update_parent(self, **kwargs):
  151.     #     sum_height = sum([child.height for child in sw.children])
  152.     #     self.height = sum_height
  153.  
  154.     def __init__(self, **kwargs):
  155.         super(steps_demo, self).__init__(**kwargs)
  156.  
  157.         for i in range(6):
  158.             # sw = StepWidget(height=250+i*10)
  159.             sw = StepWidget(size_hint_y=None)
  160.             for q in range(i):
  161.                 # tx = StepWidgetInfo()
  162.                 tx = StepWidgetInfo()
  163.                 tx.update_height = sw.update_height
  164.                 sw.bottom_layout.add_widget(tx)
  165.                 # tx.text = self.txx
  166.  
  167.             sw.update_height()
  168.             self.layout_steps.add_widget(sw)
  169.         #
  170.         # btn = Button(text='add', on_press=callback)
  171.         # self.layout_steps.add_widget(btn)
  172.  
  173.  
  174.     pass
  175.  
  176. class steps_demoApp(App):
  177.     # frame = []
  178.     # running_findtag = False
  179.     title = ''
  180.     def build(self):
  181.         # root.bind(size=self._update_rect, pos=self._update_rect)
  182.         h = 700
  183.         w = 1360
  184.         # 1305 714
  185.         Config.set('kivy', 'show_fps', 1)
  186.         Config.set('kivy', 'desktop', 1)
  187.         # Config.set('kivy', 'name', 'a')
  188.  
  189.         # Config.set('graphics', 'window_state', 'maximized')
  190.         Config.set('graphics', 'position', 'custom')
  191.         Config.set('graphics', 'height', h)
  192.         Config.set('graphics', 'width', w)
  193.         Config.set('graphics', 'top', 15)
  194.         Config.set('graphics', 'left', 4)
  195.         Config.set('graphics', 'multisamples', 0) # to correct bug from kivy 1.9.1 - https://github.com/kivy/kivy/issues/3576
  196.  
  197.  
  198.         Config.set('input', 'mouse', 'mouse,disable_multitouch')
  199.  
  200.         self.root = steps_demo()
  201.  
  202.  
  203.  
  204. if __name__ == '__main__':
  205.     steps_demoApp().run()
  206.     # comment
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement