Search code examples
pythonkivykivymd

placement of icon in center failed


i would like to place MDIcon in center of the screen, i read the documentation and followed videos and try folowing code :

`from kivymd.app import  MDApp
from kivymd.uix.label import MDLabel
from kivymd.uix.label import MDIcon
class MyApp(MDApp):
    def build(self):
        # mylabel =MDLabel(text="Hello here",halign="center",theme_text_color='Custom',
        #                  text_color=(0,0,1,1),font_style="H1")
        icon_label =MDIcon(icon='language-python',halign='center')
        return icon_label

MyApp().run()`

but when i am running the code, i am getting following result : mdicon

i am really surprised, because previous command

` # mylabel =MDLabel(text="Hello here",halign="center",theme_text_color='Custom',
        #                  text_color=(0,0,1,1),font_style="H1")`

was working fine, so what should be a reason for this?


Solution

  • The MDLabel looks centered because it fills the window. The MDIcon sets its own size to the size of the icon, so it gets positioned at the default position of (0, 0) (as does the MDLabel), but since it does not fill the window, it ends up in the bottom left corner. The fix is to position the MDIcon using position hints, like this:

    icon_label =MDIcon(icon='language-python',halign='center', pos_hint={'center_x': 0.5, 'center_y': 0.5})