Center kivy window on primary display with system with two high res monitors?

23 views
Skip to first unread message

RickF

unread,
Jun 7, 2024, 7:58:32 PMJun 7
to Kivy users support
Hello,

I'm trying to center the window on Windows 11 with a system with 2 monitors. The kivy window opens on the primary monitor, and I want to center it such that it takes up 90 percent of the screen. 

I've tried a bunch of things, but the results are either wrong or only work on a single monitor system.

If I use the screen module, it centers the window across both screens so kivy doesn't seem to know the resolution of the screen or something.

So, at the start of my app I can call:
Window.maximize()
max_size = Window.system_size

this gives me 2560 by 1369

my resolution is 3840 by 2160 , 150 %

so, kivy is saying the screen size is 2560 by 1369, which is ok, if you multiply by 1.5 you get the screen resolution....

ok so I set the Window.left to max_size[0] * 0.1 
and Window.top to max_size[1] * 0.1, and then 
Window.size to ( max_size[0] * 0.8,max_size[1] * 0.8)

Stepping through the code this seems to work in the PyCharm debugger. But the next event loop the Window.left and Window.top are reset to what they were at startup.

What am I missing here?

Thanks



ElliotG

unread,
Jun 9, 2024, 9:35:30 AMJun 9
to Kivy users support
Looks like you found a bug. You can workaround this by restoring the Window after maximizing it.

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window


kv = """
#:import Window kivy.core.window.Window

BoxLayout:
    orientation: 'vertical'
    BoxLayout:
        size_hint_y: None
        height: dp(30)
        Label:
            text: f'{Window.size=}'
        Label:
            text: f'{Window.system_size=}'
    BoxLayout:
        size_hint_y: None
        height: dp(48)
        Button:
            text: 'Maximize'
            on_release: Window.maximize()
            size_hint_y: None
            height: dp(48)
        Button:
            text: 'Scale'
            on_release: app.scale_window()
"""


class WinSizeTest(App):
    def build(self):
        return Builder.load_string(kv)

    def scale_window(self):
        Window.maximize()
        max_size = Window.system_size
        Window.restore()  # restore is required prior to setting size
        Window.size = [int(x) * .8 for x in max_size]
        Window.left, Window.top = [int(x * 0.1) for x in max_size]


WinSizeTest().run()

ElliotG

unread,
Jun 9, 2024, 10:01:51 AMJun 9
to Kivy users support
oops, minor error, 
change to:
        Window.size = [int(x * .8) for x in max_size]

Reply all
Reply to author
Forward
0 new messages