community
community copied to clipboard
Window positioning results in wrong window positions
Software Versions
- Python: 3.9.7
- OS: Manjaro Linux (KDE)
- Kivy: 2.0.0
- Kivy installation method: Manjaro community repository: python-kivy
Describe the bug
When a window is positioned via Window.left
or Window.top
then
- it is moved to a position that differs from the one specified and
- the properties top and left show position values that seem correct but differ from the specified position.
The behavior seems to be affected by the Window size.
Expected behavior
After placing a Window with Window.top
or Window.left
then the Window should be positioned
so that the outmost pixel of the top or left Window border is located at the specified screen position
of the current screen. After positioning, the properties Window.top
or Window.left
match the
previously specified values.
To Reproduce
Code and Logs and screenshots
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Window positioning test"""
from kivy.app import App
from kivy.uix.button import Button
from kivy.core.window import Window
class MyApp(App):
"""App that fails assertion when positioning the main window"""
def build(self):
Window.size = 100, 200
Window.left = 0
left_0 = Window.left
Window.left = 600
left_600 = Window.left
print(left_0, left_600)
assert (left_0, left_600) == (0, 600)
return Button(text="Test")
if __name__ in ("__android__", "__main__"):
MyApp().run()
Output
$ python kivy_window_position_test.py
[INFO ] [Logger ] Record log in /home/xxx/.kivy/logs/kivy_21-10-09_10.txt
[INFO ] [Kivy ] v2.0.0
[INFO ] [Kivy ] Installed at "/usr/lib/python3.9/site-packages/kivy/__init__.py"
[INFO ] [Python ] v3.9.7 (default, Aug 31 2021, 13:28:12)
[GCC 11.1.0]
[INFO ] [Python ] Interpreter at "/usr/bin/python"
[INFO ] [Factory ] 186 symbols loaded
[DEBUG ] [Cache ] register <kv.lang> with limit=None, timeout=None
[DEBUG ] [Cache ] register <kv.image> with limit=None, timeout=60
[DEBUG ] [Cache ] register <kv.atlas> with limit=None, timeout=None
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[DEBUG ] [Cache ] register <kv.texture> with limit=1000, timeout=60
[DEBUG ] [Cache ] register <kv.shader> with limit=1000, timeout=3600
[DEBUG ] [Text ] Provider <pango> ignored by config
[INFO ] [Text ] Provider: sdl2(['text_pango'] ignored)
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] Backend used <sdl2>
[INFO ] [GL ] OpenGL version <b'4.6 (Compatibility Profile) Mesa 21.2.3'>
[INFO ] [GL ] OpenGL vendor <b'Intel'>
[INFO ] [GL ] OpenGL renderer <b'Mesa Intel(R) Iris(R) Graphics 540 (SKL GT3)'>
[INFO ] [GL ] OpenGL parsed version: 4, 6
[INFO ] [GL ] Shading version <b'4.60'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <32>
[DEBUG ] [Shader ] Fragment compiled successfully
[DEBUG ] [Shader ] Vertex compiled successfully
[DEBUG ] [ImageSDL2 ] Load </usr/lib/python3.9/site-packages/kivy/data/glsl/default.png>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[DEBUG ] [App ] Loading kv </tmp/my.kv>
[DEBUG ] [App ] kv </tmp/my.kv> not found
6 474
Traceback (most recent call last):
File "/tmp/kivy_window_position_test.py", line 28, in <module>
MyApp().run()
File "/usr/lib/python3.9/site-packages/kivy/app.py", line 949, in run
self._run_prepare()
File "/usr/lib/python3.9/site-packages/kivy/app.py", line 919, in _run_prepare
root = self.build()
File "/tmp/kivy_window_position_test.py", line 22, in build
assert (left_0, left_600) == (0, 600)
AssertionError
Please note that Window.top
is affected as well even though this is not shown in the code example.
I've tested your code, on my client it works.
@manns
from PIL import ImageGrab
# TODO: You may know an easier way to get the size of a computer display.
resolution = ImageGrab.grab().size
# Change the values of the application window size as you need.
Config.set("graphics", "height", resolution[1])
Config.set("graphics", "width", "400")
from kivy.core.window import Window
# Place the application window on the right side of the computer screen.
Window.top = 0
Window.left = resolution[0] - Window.width
@HeaTTheatR Your example also shows strange Window behavior on my system:
from kivy.config import Config
Config.set("graphics", "height", 200)
Config.set("graphics", "width", 100)
from kivy.core.window import Window
Window.left = 300
Window.left = 300
Window.left = 300
At each successive Window.left = 300
the Window is moving down on the screen.
@manns ???
I'm having similar problems on MacOS.
When running my code in the debugger I see that the values for Window.left and Window.Top are passed as the center of the window! And Window.width and Window.height return twice the value I have set.
Config.set("graphics", "height", 101)
Config.set("graphics", "width", 41)
from kivy.core.window import Window
# At his point in the debugger: center=(41,101), width=82, height=202, left=1003, top=525
Window.top = 0
Window.left = 0
# At his point in the debugger: center=(41,101), width=82, height=202, left=0, top=53
Pretty weird.
Another confusion in all this is that (on my system) the line
resolution = ImageGrab.grab().size
returns the wrong resolution.
FYI Python 3.9.7 MacOS 12.1
this is my pyproject.toml file:
[tool.poetry]
name = "kapp"
version = "0.1.0"
description = ""
authors = ["MJM <[email protected]>"]
[tool.poetry.dependencies]
python = ">=3.9,<3.11"
Kivy-examples = "^2.0.0"
pygame = "^2.1.2"
kivy = {git = "https://github.com/kivy/kivy.git", extras = ["full"]}
kivymd = {git = "https://github.com/kivymd/KivyMD.git"}
Pillow = "^9.0.0"
[tool.poetry.dev-dependencies]
pytest = "^5.2"
Cython = "^0.29.26"
#kivy-ios = {git = "https://github.com/kivy/kivy-ios.git"}
#pyinstaller = "^4.7"
watchdog = "^2.1.6"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
from kivy.core.window import Window, WindowBase
Window.size = (width, height)
WindowBase.width = width
WindowBase.height = height
Window.width and Window.height are readonly
[I am having trouble seeing where we are at here.]
@manns: Can you please confirm this still happens with Kivy 2.2.1?
@Julian-O Yes, it still happens:
$ python kivytest.py
[INFO ] [Logger ] Record log in /home/mn/.kivy/logs/kivy_23-11-03_3.txt
[INFO ] [Kivy ] v2.2.1
[INFO ] [Kivy ] Installed at "/usr/lib/python3.11/site-packages/kivy/__init__.py"
[INFO ] [Python ] v3.11.5 (main, Sep 2 2023, 14:16:33) [GCC 13.2.1 20230801]
[INFO ] [Python ] Interpreter at "/usr/bin/python"
[INFO ] [Logger ] Purge log fired. Processing...
[INFO ] [Logger ] Purge finished!
[INFO ] [Factory ] 190 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2(['text_pango'] ignored)
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] Backend used <sdl2>
[INFO ] [GL ] OpenGL version <b'4.6 (Compatibility Profile) Mesa 23.1.9-manjaro1.1'>
[INFO ] [GL ] OpenGL vendor <b'Intel'>
[INFO ] [GL ] OpenGL renderer <b'Mesa Intel(R) Iris(R) Graphics 540 (SKL GT3)'>
[INFO ] [GL ] OpenGL parsed version: 4, 6
[INFO ] [GL ] Shading version <b'4.60'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
6 474
Traceback (most recent call last):
File "/tmp/kivytest.py", line 28, in <module>
MyApp().run()
File "/usr/lib/python3.11/site-packages/kivy/app.py", line 955, in run
self._run_prepare()
File "/usr/lib/python3.11/site-packages/kivy/app.py", line 925, in _run_prepare
root = self.build()
^^^^^^^^^^^^
File "/tmp/kivytest.py", line 22, in build
assert (left_0, left_600) == (0, 600)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError
However, the issue from my 2nd comment with
Window.left = 300
seems to be fixed.
@mjmare:
Window.width and Window.height return twice the value I have set.
That sounds like a HiDPI issue with OSX. I have seen a few issues related to that. I suspect unrelated to the original issue here, but I haven't the expertise to be definitive.
Window.width and Window.height return twice the value I have set.
This has been explained in #8630 (and duplicated of #8140 ).
Please also take a look at https://kivy.org/doc/master/api-kivy.core.window.html#kivy.core.window.WindowBase.system_size and https://kivy.org/doc/master/api-kivy.core.window.html#kivy.core.window.WindowBase.size.
The original issue doesn't seem to be replicable on the latest master
.
Please reach out here if the original issue still happens on your side.
@misl6 The behavior is still the same for 2.3.0
$ python kivy_window_position_test.py
[INFO ] [Logger ] Record log in /home/mn/.kivy/logs/kivy_24-03-20_6.txt
[INFO ] [Kivy ] v2.3.0
[INFO ] [Kivy ] Installed at "/home/mn/.local/lib/python3.11/site-packages/kivy/__init__.py"
[INFO ] [Python ] v3.11.8 (main, Feb 12 2024, 14:50:05) [GCC 13.2.1 20230801]
[INFO ] [Python ] Interpreter at "/usr/bin/python"
[INFO ] [Logger ] Purge log fired. Processing...
[INFO ] [Logger ] Purge finished!
[INFO ] [Factory ] 195 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] Backend used <sdl2>
[INFO ] [GL ] OpenGL version <b'4.6 (Compatibility Profile) Mesa 24.0.2-manjaro1.1'>
[INFO ] [GL ] OpenGL vendor <b'Intel'>
[INFO ] [GL ] OpenGL renderer <b'Mesa Intel(R) Iris(R) Graphics 540 (SKL GT3)'>
[INFO ] [GL ] OpenGL parsed version: 4, 6
[INFO ] [GL ] Shading version <b'4.60'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
6 474
Traceback (most recent call last):
File "/home/mn/Downloads/kivy_window_position_test.py", line 28, in <module>
MyApp().run()
File "/home/mn/.local/lib/python3.11/site-packages/kivy/app.py", line 955, in run
self._run_prepare()
File "/home/mn/.local/lib/python3.11/site-packages/kivy/app.py", line 925, in _run_prepare
root = self.build()
^^^^^^^^^^^^
File "/home/mn/Downloads/kivy_window_position_test.py", line 22, in build
assert (left_0, left_600) == (0, 600)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError