Reputation: 213
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
Reputation: 16031
snake = Snake()
snake.pos = self.width - snake.width, self.y
print("width={0}, pos={1}".format(snake.width, snake.pos))
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)
size_hint: None, None
to override widget's default size: (100, 100)
or default size_hint: (1, 1)
.<Snake>:
size_hint: None, None
size: 20, 20
- 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.
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
If you don’t want to use a
size_hint
for either the width or height, set the value to None.
Widget
with FloatLayout
pos_hint={'right': 1}
class SnakeGame(FloatLayout):
def __init__(self, **kwargs):
super(SnakeGame, self).__init__(**kwargs)
self.add_widget(Snake(pos_hint={'right': 1}))
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.
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()
#: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
Upvotes: 2