Advertisement
2n2u

Untitled

Apr 11th, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.31 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from kivy.clock import Clock
  3. from kivy.lang import Builder
  4. from kivy.metrics import dp
  5. from kivy.properties import ListProperty, StringProperty, OptionProperty, ObjectProperty
  6. from kivy.uix.boxlayout import BoxLayout
  7. from kivymd.backgroundcolorbehavior import BackgroundColorBehavior
  8. from kivymd.button import MDIconButton
  9. from kivymd.theming import ThemableBehavior
  10. from kivymd.elevationbehavior import ElevationBehavior
  11.  
  12. Builder.load_string('''
  13. #:import m_res kivymd.material_resources
  14. <Toolbar>
  15.    tabs_layout: tabs_layout
  16.    # tab1: tab1
  17.    # tab2: tab2
  18.    size_hint_y: None
  19.    height: root.theme_cls.standard_increment
  20.    background_color: root.theme_cls.primary_color
  21.    padding: [root.theme_cls.horizontal_margins - dp(12), 0]
  22.    opposite_colors: True
  23.    elevation: 6
  24.    BoxLayout:
  25.        orientation: 'vertical'
  26.        BoxLayout:
  27.            orientation: 'horizontal'
  28.            BoxLayout:
  29.                id: left_actions
  30.                orientation: 'horizontal'
  31.                size_hint_x: None
  32.                padding: [0, (self.height - dp(48))/2]
  33.            BoxLayout:
  34.                padding: dp(12), 0
  35.                MDLabel:
  36.                    font_style: 'Title'
  37.                    opposite_colors: root.opposite_colors
  38.                    theme_text_color: root.title_theme_color
  39.                    text_color: root.title_color
  40.                    text: root.title
  41.                    shorten: True
  42.                    shorten_from: 'right'
  43.            BoxLayout:
  44.                id: right_actions
  45.                orientation: 'horizontal'
  46.                size_hint_x: None
  47.                padding: [0, (self.height - dp(48))/2]
  48.        BoxLayout:
  49.            id: tabs_layout
  50.            orientation: 'horizontal'
  51.            # canvas.after:
  52.            #     #Color: (1, 0, 0, 1)
  53.            #     Rectangle:
  54.            #         group: 'rectangle'
  55.            #         pos: self.x, self.y
  56.            #         #size: root.temp_width, dp(4)
  57.            # MDFlatButton:
  58.            #     id: tab1
  59.            #     name: 'tab1'
  60.            #     theme_text_color: 'Custom'
  61.            #     opposite_colors: True
  62.            # MDFlatButton:
  63.            #     id: tab2
  64.            #     name: 'tab2'
  65.            #     theme_text_color: 'Custom'
  66.            #     opposite_colors: True
  67. ''')
  68.  
  69.  
  70. class Toolbar(ThemableBehavior, ElevationBehavior, BackgroundColorBehavior,
  71.               BoxLayout):
  72.     left_action_items = ListProperty()
  73.     """The icons on the left of the Toolbar.
  74.  
  75.    To add one, append a list like the following:
  76.  
  77.        ['icon_name', callback]
  78.  
  79.    where 'icon_name' is a string that corresponds to an icon definition and
  80.     callback is the function called on a touch release event.
  81.    """
  82.  
  83.     right_action_items = ListProperty()
  84.     """The icons on the left of the Toolbar.
  85.  
  86.    Works the same way as :attr:`left_action_items`
  87.    """
  88.  
  89.     title = StringProperty()
  90.     """The text displayed on the Toolbar."""
  91.  
  92.     title_theme_color = OptionProperty(None, allownone=True,
  93.                                        options=['Primary', 'Secondary', 'Hint',
  94.                                                 'Error', 'Custom'])
  95.  
  96.     title_color = ListProperty(None, allownone=True)
  97.  
  98.     tab1 = ObjectProperty()
  99.  
  100.     def __init__(self, **kwargs):
  101.         super(Toolbar, self).__init__(**kwargs)
  102.         Clock.schedule_once(
  103.             lambda x: self.on_left_action_items(0, self.left_action_items))
  104.         Clock.schedule_once(
  105.             lambda x: self.on_right_action_items(0,
  106.                                                  self.right_action_items))
  107.  
  108.     def on_left_action_items(self, instance, value):
  109.         self.update_action_bar(self.ids['left_actions'], value)
  110.  
  111.     def on_right_action_items(self, instance, value):
  112.         self.update_action_bar(self.ids['right_actions'], value)
  113.  
  114.     def update_action_bar(self, action_bar, action_bar_items):
  115.         action_bar.clear_widgets()
  116.         new_width = 0
  117.         for item in action_bar_items:
  118.             new_width += dp(48)
  119.             action_bar.add_widget(MDIconButton(icon=item[0],
  120.                                                on_release=item[1],
  121.                                                opposite_colors=True))
  122.         action_bar.width = new_width
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement