I'm creating a game in kivy and noticed that the window size is always 25% bigger than I specify it to be:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
# screen dimensions, px
screen_width = 400
screen_height = 600
Window.size = (screen_width, screen_height)
class PlayGame(Widget):
def on_touch_move(self, touch):
self.touch = touch
print(self.touch.x, self.touch.y)
class TestGame(App):
def build(self):
return PlayGame()
if __name__ == '__main__':
game = TestGame()
game.run()
Running this code, then clicking my mouse and running it down the side of the screen gives a width of 500 and doing the same along the top gives a height of 750.
I have looked in the kivy docs, but all it has for the window size is
Get the rotated size of the window. If rotation is set, then the size will change to reflect the rotation.
New in version 1.0.9.
size is an AliasProperty.
I'm not sure that any of this helps, expecially given that the window is not rotated.
When you change a Window.size
, kivy triggers some functions to update the window size. This part of the code in kivy is the reason behind your problem.
file ..\Python\Python39\site-packages\kivy\core\window\__init__.py", line 420
def _get_size(self):
r = self._rotation
w, h = self._size
if platform == 'win' or self._density != 1:
w, h = self._win._get_gl_size()
if self.softinput_mode == 'resize':
h -= self.keyboard_height
if r in (0, 180):
return w, h
return h, w
if your system is windows based, window size is changed by this function self._win._get_gl_size()
. You cannot stop it from changing it unless you edit kivy code or platform value, which is not recommended and may cause serious problems.
One solution is to find the scale and multiply it to x and y values.
screen_width = 400
screen_height = 600
Window.size = (screen_width, screen_height)
scale = Window._size[0] / Window.size[0]
print(Window._size, Window.size, scale)
class PlayGame(Widget):
def on_touch_move(self, touch):
self.touch = touch
print(self.touch.x * scale, self.touch.y * scale)