G.Brown
G.Brown

Reputation: 213

How can I change default values of kivy widgets?

Although it seems like a pretty task, I can not figure out, how to configure for example Widget.width.defaultvalue in kivy.

The widget I have in mind is defined as follows in the .kv file:

<Snake>:
    size: 20, 20
    canvas:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

This Snake Widget is in principal nothing but:

class Snake(Widget):
    pass

However, when I wanted to add such a Snake in my python code, trying to position it in the bottom right corner, I found out, that the width defaultvalue is 100 instead of the 20 I defined in the .kv file.

class SnakeGame(Widget):

def __init__(self):
    super().__init__()
    self.add_widget(Snake(pos=(self.width - Snake.width.defaultvalue, self.y)))

For my snake class, I tried

super.width.defaultvalue = 20

which led to an error. Could anybody give me a hint, where I can change this defaultvalue?

Best regards!

EDIT: The actual answer to my question is almost stupid simple:

class Snake(Widget):
        width = NumericProperty(20)

Upvotes: 1

Views: 1016

Answers (1)

ikolim
ikolim

Reputation: 16031

Access Snake widget's width

Python code

  1. Instantiate Snake object, snake = Snake()
  2. Set Snake object position, snake.pos = self.width - snake.width, self.y
  3. Add print() function to verify snake's width and position, print("width={0}, pos={1}".format(snake.width, snake.pos))
  4. Add snake widget/object to root widget.

Snippet - Python code

class SnakeGame(FloatLayout):

    def __init__(self, **kwargs):
        super(SnakeGame, self).__init__(**kwargs)
        snake = Snake()
        snake.pos = self.width - snake.width, self.y
        print("width={0}, pos={1}".format(snake.width, snake.pos))
        self.add_widget(snake)

kv file

  1. Add size_hint: None, None to override widget's default size: (100, 100) or default size_hint: (1, 1).

Snippet - kv file

<Snake>:
    size_hint: None, None
    size: 20, 20

Widget » default size

  • The default size of a widget is (100, 100).
  • The default size_hint is (1, 1).

Please refer to the snippet and example for details.

Snake - size_hint

Add size_hint: None, None to override widget's default size: (100, 100) or size_hint: (1, 1).

<Snake>:
    size_hint: None, None
    size: 20, 20

Widget » size_hint

If you don’t want to use a size_hint for either the width or height, set the value to None.

SnakeGame - pos_hint

  1. Replace Widget with FloatLayout
  2. Use pos_hint={'right': 1}

Snippet

class SnakeGame(FloatLayout):

    def __init__(self, **kwargs):
        super(SnakeGame, self).__init__(**kwargs)
        self.add_widget(Snake(pos_hint={'right': 1}))

Widget » pos_hint

pos_hint

Position hint. This property allows you to set the position of the widget inside its parent layout, in percent (similar to size_hint).

For example, if you want to set the top of the widget to be at 90% height of its parent layout, you can write:

widget = Widget(pos_hint={'top': 0.9})

The keys ‘x’, ‘right’ and ‘center_x’ will use the parent width. The keys ‘y’, ‘top’ and ‘center_y’ will use the parent height.

See Float Layout for further reference.

Note

pos_hint is not used by all layouts. Check the documentation of the layout in question to see if it supports pos_hint.

pos_hint is an ObjectProperty containing a dict.

Example

main.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout


class Snake(Widget):
    pass


class SnakeGame(FloatLayout):

    def __init__(self, **kwargs):
        super(SnakeGame, self).__init__(**kwargs)
        self.add_widget(Snake(pos_hint={'right': 1}))


class TestApp(App):
    title = "Kivy - Snake Game"

    def build(self):
        return SnakeGame()


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

test.kv

#:kivy 1.11.0

<Snake>:
    size_hint: None, None
    size: 20, 20
    canvas:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

Output

Img01

Upvotes: 2

Related Questions