Search code examples
layoutkivykivy-language

How can I define widget width relative to the window in kv language?


I am trying to create the layout pictured below in a kv file. I have tried several different methods to define a 30% region containing a label, but the label always ends up in the bottom left corner of the screen. I suspect I'm misusing size, size_hint and text_size, and/or that I need a containing element for the label (I've tried FloatLayout).

sample.kv

#:kivy 1.0.9

<SampleGui>:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: "background.jpeg"

    Label:
        text: "Illum qui labore quia sed quia sint quaerat."
        size_hint: (0.3, 1)
        pos_hint: {'right': 1}

sample.py

from kivy.app import App
from kivy.config import Config
from kivy.uix.widget import Widget

Config.set('graphics', 'window_state', 'maximized')


class SampleGui(Widget):
    pass


class SampleApp(App):
    def build(self):
        return SampleGui()


if __name__ == '__main__':
    SampleApp().run()

How can I define allocate a 30% width to the label? (Padding and other details are not important, unless they impact the answer.)

Wireframe of a maximised window with a 30% label on the right.


Solution

  • And with the added code, your error becomes obvious. Your SampleGui extends Widget, but the child Label uses attributes (like size_hint and pos_hint) that are only supported by Layout classes. Try changing your SampleGui to extend FloatLayout instead of Widget.