kivy understand self.pos and self.size

  • Last Update :
  • Techknowledgy :

Whenever I try to span the canvas across the layout, all I have to do is this

<FloatLayout>:
   canvas:
   Color:
   rgba: 54 / 255, 60 / 255, 72 / 255, 1
   Rectangle:
   pos: self.pos
   size: self.size

Suggestion : 2

The default size of a widget is (100, 100). This is only changed if the parent is a Layout. For example, if you add a Label inside a Button, the label will not inherit the button’s size or position because the button is not a Layout: it’s just another Widget.,The default size_hint is (1, 1). If the parent is a Layout, then the widget size will be the parent layout’s size.,Position hint. This property allows you to set the position of the widget inside its parent layout (similar to size_hint).,A Widget is not a Layout: it will not change the position or the size of its children. If you want control over positioning or sizing, use a Layout.

<name> is a <property class> and defaults to <default value>.
def callback_pos(instance, value):
   print('The widget', instance, 'moved to', value)

wid = Widget()
wid.bind(pos = callback_pos)
def redraw(self, args):
   self.bg_rect.size = self.size
self.bg_rect.pos = self.pos

widget = Widget()
with widget.canvas:
   widget.bg_rect = Rectangle(source = "cover.jpg", pos = self.pos, size = self.size)
widget.bind(pos = redraw, size = redraw)
Widget:
   canvas:
   Rectangle:
   source: "cover.jpg"
size: self.size
pos: self.pos
box = BoxLayout()
box.add_widget(Label(text = "a"))
box.add_widget(Label(text = "b"))
box.add_widget(Label(text = "c"))
box = BoxLayout()
box.add_widget(Label(text = "a"), index = 0)
box.add_widget(Label(text = "b"), index = 1)
box.add_widget(Label(text = "c"), index = 2)

Suggestion : 3

Last Updated : 19 Oct, 2021,GATE CS 2021 Syllabus

pos_hint : Provide hint of position. We can define upto 8 keys i.e. it takes arguments in form of dictionary. 
 

pos_hint = {
      “
      x”: 1,
      “y”: 1,
      “left”: 1,
      “right”: 1,
      "center_x": 1,
      "center_y": 1,
      "top”:1, “bottom”:1("
      top ":0)}

pos_hint : Provide hint of position. We can define upto 8 keys i.e. it takes arguments in form of dictionary. 
 

pos_hint = {
      “
      x”: 1,
      “y”: 1,
      “left”: 1,
      “right”: 1,
      "center_x": 1,
      "center_y": 1,
      "top”:1, “bottom”:1("
      top ":0)}

 

Basic Approach:

   1) import kivy
2) import kivyApp
3) import all neaded(like button and layouts to use them)
4) Set minimum version(optional)
5) create Layout class
6) create App class
7) Set up the position and size of the buttons
8) return Layout / widget / Class(according to requirement)
9) Run an instance of the class