Search code examples
pythonkivy

How can I position my kivy app window in the right bottom corner


I have a kivy desktop app and want to start it in the corner at the right. Currently, i can only apply the position from the left, top...

from kivy.config import Config
Config.set('graphics','position','custom')
Config.set('graphics','resizable',0)
Config.set('graphics','left',20)
Config.set('graphics','top',50)
Config.write()

Solution

  • I believe that using the left and top properties are the only way to position an app. If that is true, then you must get the size of the device screen, and use that to calculate the the top and left. However, getting the size of the device screen is difficult in python/kivy. You can get an approximation by maximizing the kivy window and using its size. Here is some code that follows this approach:

    def do_maximize(self, dt):
        self.app_size = Window.size
        Window.maximize()
        Clock.schedule_once(self.adjust_window)
    
    def adjust_window(self, dt):
        window_size = Window.size
        top = Window.top
        left = Window.left
        Window.restore()
        Window.top = window_size[1] - self.app_size[1] + top
        Window.left = window_size[0] - self.app_size[0] + left
    

    And calling:

        Clock.schedule_once(self.do_maximize)
    

    from the App.build() method should approximate what you want.

    A more accurate, but more complex approach would be to use subprocess to run a command to capture the device size (something like xdpyinfo on Linux) and parse the output to get the size.