Can KivyMD toolbar title be centered---even when other items are added?

594 views
Skip to first unread message

Condado

unread,
Nov 3, 2021, 6:42:23 PM11/3/21
to Kivy users support
I'm trying to keep a title centered in the KivyMD toolbar---even if action items (like a back button icon) are added to the toolbar. In the code below, adding the "chevron-left" icon on the left side shifts "MyTitle" off-center to the right. How can I keep the toolbar title centered in an absolute way no matter what other icons, labels or buttons are added to the toolbar? Thanks.

Kivy 

Screen:
    name: "primary_navigation"

    MDBoxLayout:
        orientation: "vertical"

        MDToolbar:
            title: "MyTitle"
            anchor_title: "center"
            md_bg_color: .5,.5,.5,.1
            specific_text_color: 1,1,1,.9
            left_action_items: [["chevron-left", lambda x: x]]

Elliot Garbus

unread,
Nov 4, 2021, 5:44:02 PM11/4/21
to kivy-...@googlegroups.com

The straight forward way to do it:  Add a right action item the same size as your left action time.  This will cause the area for the Label to be centered.

 

A little more complicated.  I used the same concept but force the size of the area used for the right action items to match the size of the left action times.    You can see the kv code for the MDToolbar here: https://github.com/kivymd/KivyMD/blob/master/kivymd/uix/toolbar/toolbar.kv

 

I used the id’s in the toolbar to dynamically make the modifications.  I added a label to your example to make it easy to see the true center.

 

from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.toolbar import MDToolbar
from kivy.properties import BooleanProperty

kv =
"""
MDBoxLayout:
    orientation: "vertical"
    Label
        size_hint_y: None
        height: 24
        color: 'black'
        text: 'Center'
    CenteredMDToolbar:

        title: "MyTitle"
        anchor_title: 'center'
        md_bg_color: .5,.5,.5,.1
        specific_text_color: 1,1,1,.9
        left_action_items: [["chevron-left", lambda x: x]]
        # right_action_items: [["chevron-left", lambda x: x]]
"""


class CenteredMDToolbar(MDToolbar):
   
def on_kv_post(self, base_widget):
       
self.ids.left_actions.bind(size=self.re_center)

   
def re_center(self, obj, v):
       
self.ids.right_actions.width = self.ids.left_actions.width


class CenterTaskBarApp(MDApp):
   
def build(self):
       
return Builder.load_string(kv)


CenterTaskBarApp().run()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/f3f1e221-c509-400a-ae57-0a2aede0b609n%40googlegroups.com.

 

Condado

unread,
Nov 4, 2021, 7:56:18 PM11/4/21
to Kivy users support
Brilliant, thank you Elliot. I incorporated that second method and I think that will work!
Reply all
Reply to author
Forward
0 new messages