floatlayout pos_hint, size_hint issues...

221 views
Skip to first unread message

Johannes Vogel

unread,
Dec 26, 2016, 5:18:00 AM12/26/16
to Kivy users support
Hi Again everyone,

I have a question with regards to the floatlayout. Below you can find a minimal example of it, where I try to create a widget that draws an ellipse to its canvas.

Now whatever I post in the pos_hint or size_hint, makes no difference. It only uses default size (100, 100 I assume) and default pos (0,0). absolute positioning and sizing seems to work. I find this very strange.

Can anyone of you help me with that?

Thanks :)

Johannes

NOTE:
I am using kivy 1.9.1 with python 3.5.2 in ubuntu 16.04

Please also note, that I do not use the kv language for this, because for my purposes, I need to dynamically create these widgets as the game progresses. I hope that makes sense....

The code:

#!usr/bin/python3

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color, Ellipse


class Dot(Widget):
    def __init__(self, **kwargs):
        super(Dot, self).__init__(**kwargs)

        with self.canvas:
            Color(255, 255, 255)
            self.ell = Ellipse(pos=self.pos, size=self.size)


class SchemeGame(FloatLayout):
    def __init__(self,**kwargs):
        super(SchemeGame, self).__init__(**kwargs)

        # Draw the ellipse widgets
        e = Dot(pos_hint={'x': 1, 'y': .5}, size_hint=(0.5, 0.5))
        self.add_widget(e)

class SchemeApp(App):
    def build(self):
        game = SchemeGame()
        return game

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


Thanks! :)


ZenCODE

unread,
Dec 26, 2016, 12:10:32 PM12/26/16
to Kivy users support
You need to bind to the size and pos changes.

https://kivy.org/docs/guide/graphics.html#manipulating-instructions

Johannes Vogel

unread,
Dec 27, 2016, 3:18:27 AM12/27/16
to Kivy users support
Hin ZenCODE,

Thanks a lot for your input! I am having trouble wrapping my head around a couple of kivy concepts mainly because on the surface there is endless loop like is advocated in for example pygame.

Anyhow, I tried the changes based on the code snippet you linked to, but unfortunately now the screen is just black. I posted below the entire minimal example with the changes that I made based on the link.

Could you again have a look and let me know what I am doing wrong this time? Thanks so much :)

Minimal Example:
=============


#!usr/bin/python3

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color, Ellipse


class Dot(Widget):
    def __init__(self, **kwargs):
        super(Dot, self).__init__(**kwargs)

        with self.canvas:
            Color(255, 255, 255)
            self.ell = Ellipse(pos=self.pos, size=self.size)
       
        self.bind(pos=self.update)
        self.bind(size=self.update)

    def update(self, *args):
        self.ell.pos = self.pos
        self.ell.size = self.size




class SchemeGame(FloatLayout):
    def __init__(self,**kwargs):
        super(SchemeGame, self).__init__(**kwargs)

        # Draw the ellipse widgets
        e = Dot(pos_hint={'x': 1, 'y': .5}, size_hint=(0.5, 0.5))
        self.add_widget(e)


class SchemeApp(App):
    def build(self):
        game = SchemeGame()
        return game

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

ZenCODE

unread,
Dec 28, 2016, 11:46:05 AM12/28/16
to Kivy users support
Make the pos_hint with {'x': 0, y: .5}. x =1 is off the right hand side of the screen. :-)

Johannes Vogel

unread,
Dec 29, 2016, 5:30:11 AM12/29/16
to Kivy users support
Ummm.... D'oh! :-D Thanks!
Reply all
Reply to author
Forward
0 new messages