Budaksesat2000
Budaksesat2000

Reputation: 111

Kivy widget rectangle not moving

Hi there when i run my code , it is supposed to show a rectangle shape moving up and down. However, i can only get the rectangle shape in static mode (not moving). However, according to the function under the widget class, it should work. Any help will be aprreciated !

countdown.py file

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.widget import Widget
from kivy.graphics import *
from kivy.clock import Clock
from kivy.uix.screenmanager import Screen, ScreenManager

class WindowManager(ScreenManager):
    pass

class GameWindow(Screen):
    def __init__(self, **kwargs):
        super(GameWindow, self).__init__(**kwargs)
        self.add_widget(Root())
        self.add_widget(Rect())


class Root(Widget):
    pass

class Rect(Widget):
    velocity = ListProperty([10, 2])

    def __init__(self, **kwargs):
        super(Rect, self).__init__(**kwargs)
        Clock.schedule_interval(self.update, 1/8)

    def update(self, *args):

        self.y += self.velocity[1]
        if self.y < 0 or (self.y + self.height) > 250:
            self.velocity[1] *= -1


kys = Builder.load_file("countdown.kv")

class MyMainApp(App):
    def build(self):
        return kys

if __name__ == "__main__":
    MyMainApp().run()

countdown.kv file

WindowManager:
    GameWindow:

<GameWindow>:
    canvas:
        Color:
            rgba: 1, 0, 0, 1
        Rectangle:
            pos: self.pos
            size: (42,15)


    Rect:
        pos: 85, 100

Upvotes: 0

Views: 202

Answers (1)

eyllanesc
eyllanesc

Reputation: 243887

A Widget has some of the following characteristics:

  • If you are placed as a child of another Widget (Screen, Spinner, etc) this will take as much space as you can since the size_hint is (1,1), in your case it occupies the entire space of the Screen.

  • It cannot be distinguished since it does not have a background color or other visual element.

With the above mentioned it follows that even when "Rect" which is a Widget was moved this cannot be distinguished so we must use the canvas for its painting (I suppose that is what you tried when using the canvas in GameWindow), in addition to set a container independent size should be set the size_hint to (None, None).

Considering the above, the solution is:

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.widget import Widget
from kivy.clock import Clock
from kivy.uix.screenmanager import Screen, ScreenManager


class WindowManager(ScreenManager):
    pass


class GameWindow(Screen):
    pass


class Rect(Widget):
    velocity = ListProperty([10, 2])

    def __init__(self, **kwargs):
        super(Rect, self).__init__(**kwargs)
        Clock.schedule_interval(self.update, 1. / 8.)

    def update(self, *args):
        self.y += self.velocity[1]
        if self.y < 0 or (self.y + self.height) > 250:
            self.velocity[1] *= -1


kys = Builder.load_file("countdown.kv")


class MyMainApp(App):
    def build(self):
        return kys


if __name__ == "__main__":
    MyMainApp().run()
WindowManager:
    GameWindow:

<GameWindow>:
    Rect:

<Rect>:
    size_hint : None, None
    size : 42, 15
    canvas:
        Color:
            rgba: 1, 0, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size

Upvotes: 1

Related Questions