community icon indicating copy to clipboard operation
community copied to clipboard

Widget positioning issue when using custom Widget as parent

Open John-Glazebrook opened this issue 1 year ago • 1 comments

Description: I encountered an issue where child widgets were not being positioned correctly within a custom Widget class. Specifically, when using pos_hint and size_hint on a BoxLayout inside a custom Compass widget (which initially inherited from Widget), the BoxLayout appeared in the bottom-left corner instead of being centered as intended.

The issue was resolved by changing the Compass class to inherit from FloatLayout instead of Widget. This suggests that there might be a bug in how Kivy handles layout properties for child widgets when the parent is a custom Widget class.

Original code (not working as expected): class Compass(Widget): # ... (rest of the class definition)

<Compass>: BoxLayout: size_hint: 0.9, 0.9 pos_hint: {'center_x': 0.5, 'center_y': 0.5} # ... (rest of the BoxLayout definition)

Workaround (working correctly): class Compass(FloatLayout): # ... (rest of the class definition)

<Compass>: BoxLayout: size_hint: 0.9, 0.9 pos_hint: {'center_x': 0.5, 'center_y': 0.5} # ... (rest of the BoxLayout definition)

Environment:

  • Kivy version: v2.3.0
  • Python version: 3.12.4
  • Operating System: Win 11

I would appreciate if the Kivy team could investigate this behavior and clarify if this is a bug or if there's a recommended way to handle custom Widget classes that need to position their children using pos_hint and size_hint.

John-Glazebrook avatar Jul 02 '24 14:07 John-Glazebrook

This is not a bug. Layouts are tools (special widgets) for positioning and sizing widgets (their children). I you want to create a Widget where the Widgets children are positioned with sized with hints, use a RelativeLayout or a FloatLayout. These layouts honor the hints.

A horizontal BoxLayout, for example, will not honor 'x' hints. the layout positions the widgets in that dimension. Read the docs about Layouts carefully, this behavior is described. https://kivy.org/doc/stable/api-kivy.uix.boxlayout.html#module-kivy.uix.boxlayout

In the kivy-examples directory, there is an application called kivycalatog. .venv/share/kivy-examples/demo/kivycatalog/main.py It provides an interactive kv authoring tool that lets you explore the layouts interactively. I found this to be very useful when I was getting started.

The github issues section is for reporting bugs. There are forums for support: https://groups.google.com/g/kivy-users https://discord.com/invite/eT3cuQp https://www.reddit.com/r/kivy/

ElliotGarbus avatar Jul 02 '24 15:07 ElliotGarbus

👋 We use the issue tracker exclusively for bug reports and feature requests. However, this issue appears to be a support request. Please use our support channels to get help with the project.

If you're having trouble installing Kivy, make sure to check out the installation docs for Windows, Linux and macOS.

Let us know if this comment was made in error, and we'll be happy to reopen the issue.

github-actions[bot] avatar Dec 26 '24 08:12 github-actions[bot]