Joshua Harwood
Joshua Harwood

Reputation: 357

How Do I Get Size Hints to Apply to Layout Elements, Not the Window, in Kivy

I've got the following code with a sample window size. Instead of partitioning out percentages of the window, I want all of the widgets to go into a BoxLayout called inlay. However, no matter where I put the hints or how I append the widgets, the hints are still applying to the whole window.

As the code will show, for a window of (500, 600), the inlay should only be of size (480, 600), the nearest 8 x 10 dimension. However, the hints are rendered for the (500, 600) size:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window

Window.size = (500, 600)

class SCApp(App):
    def build(self):
        # Define the (relative) 8 x 10 inlay for the board.
        for n in range(8, Window.width, 8):
            if n * 1.25 <= Window.height:
                inlay_dims = (n, int(n * 1.25))
                print(inlay_dims)
            else:
                break
        inlay = BoxLayout(orientation="vertical")
        inlay.size = inlay_dims
        top_portion = BoxLayout(orientation="horizontal")
        inlay.add_widget(top_portion)
        top_portion.size_hint = (1, 0.1)
        top_portion.add_widget(widget=Button())
        mid_portion = BoxLayout(orientation="horizontal")
        inlay.add_widget(mid_portion)
        mid_portion.size_hint = (1, 0.8)
        btm_portion = BoxLayout(orientation="horizontal")
        inlay.add_widget(btm_portion)
        btm_portion.size_hint = (1, 0.1)
        top_portion.add_widget(widget=Button())
        return inlay

SCApp().run()

How do I make the _portion layouts' size hints evaluate to the size given for inlay?

Upvotes: 0

Views: 182

Answers (1)

John Anderson
John Anderson

Reputation: 39012

When you use size, you generally must also use size_hint=(None, None) as size_hint will over-ride size settings. So try replacing:

    inlay = BoxLayout(orientation="vertical")

with:

    inlay = BoxLayout(orientation="vertical", size_hint=(None, None))

Upvotes: 1

Related Questions