Widgets do not update?

2,294 views
Skip to first unread message

duelafn

unread,
Mar 28, 2012, 10:31:05 PM3/28/12
to Kivy users support
Am I missing something important in this code? Running the following
script produces a window with a translucent 40x40 Rectangle in the
lower left corner ( as in with x=0, y=0 even though I set "center =
(100,100)" ). The Animation "happens" (as in the GoStone updates
its .pos), but no motion is visible on the screen - the Rectangle just
sits in the lower-left corner.

I am able to run all of the scripts in the kivy examples folder and
they work fine. What's wrong with my script?

Thanks,
Dean


import kivy
from kivy.app import App
from kivy.graphics import Color, Rectangle
from kivy.uix.widget import Widget
from kivy.animation import Animation
from kivy.clock import Clock

class GoGame(Widget):
def __init__(self, **kwargs):
super(GoGame, self).__init__(**kwargs)
self.stone = GoStone(size=(40,40))
self.stone_info()
self.stone.center = (100,100)
self.stone_info()
anim = Animation(x=500, y=500, duration=1)
anim.start(self.stone)
Clock.schedule_once(lambda dt: self.stone_info(), 1)
self.add_widget( self.stone )

def stone_info(self):
print "pos:", self.stone.pos, "size:", self.stone.size

class GoStone(Widget):
def __init__(self, **kwargs):
super(GoStone, self).__init__(**kwargs)
with self.canvas:
Color(1,1,1,.5)
Rectangle(pos=self.pos, size=self.size)

class TTGoApp(App):
def build(self):
return GoGame()

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



Output:

[INFO ] Kivy v1.1.1
[INFO ] [Logger ] Record log in /home/duelafn/.kivy/logs/
kivy_12-03-28_33.txt
[INFO ] [Factory ] 102 symbols loaded
[INFO ] [Window ] using <pygame> as window provider
[INFO ] [GL ] OpenGL version <3.3.10666 Compatibility
Profile Context>
[INFO ] [GL ] OpenGL vendor <ATI Technologies Inc.>
[INFO ] [GL ] OpenGL renderer <ATI Mobility Radeon HD 4500
Series>
[INFO ] [GL ] Shading version <3.30>
[INFO ] [GL ] Texture max size <8192>
[INFO ] [GL ] Texture max units <16>
[INFO ] [Shader ] fragment shader: <Fragment shader was
successfully compiled to run on hardware.>
[INFO ] [Shader ] fragment compiled successfully
[INFO ] [Shader ] vertex shader: <Vertex shader was
successfully compiled to run on hardware.>
[INFO ] [Shader ] vertex compiled successfully
[INFO ] [Shader ] program: <Vertex shader(s) linked, fragment
shader(s) linked.>
[INFO ] [Window ] virtual keyboard not allowed, single mode,
not docked
pos: [0, 0] size: [40, 40]
pos: [80.0, 80.0] size: [40, 40]
[INFO ] [OSC ] using <multiprocessing> for socket
[INFO ] [Base ] Start application main loop
pos: [500.0, 500.0] size: [40, 40]
[INFO ] [Base ] Leaving application in progress...

Gabriel Pettier

unread,
Mar 29, 2012, 5:04:50 AM3/29/12
to kivy-...@googlegroups.com
i think you want to put the canvas instructions in a separate method, and call it when pos/size change:

class GoStone(Widget): 
    def update_canvas(self, *args):
        self.canvas.clear()
        with self.canvas: 
            Color(1,1,1,.5) 
            Rectangle(pos=self.pos, size=self.size) 

    def __init__(self, **kwargs): 
        super(GoStone, self).__init__(**kwargs) 
        self.update_canvas()
        self.bind(pos=update_canvas)
        self.bind(size=update_canvas)

 
because on the python size, canvas instructions are not automatically updated when properties changes, (the instruction get the current value, which doesn't change), if you did this on the kv side, you'd get bindings automatically.

Mathieu Virbel

unread,
Mar 29, 2012, 6:11:38 AM3/29/12
to kivy-...@googlegroups.com
This part of the doc describe exactly your case:
http://kivy.org/docs/guide/kvlang.html

Mathieu

duelafn

unread,
Mar 29, 2012, 9:19:52 AM3/29/12
to Kivy users support

Ah, thank you both - I guess I deserve the red face for ignoring a
core feature of the language (I hadn't quite understood the benefit of
moving things to a .kv file so I was ignoring those pages of the
documentation).


My work in progress game is at: https://github.com/duelafn/tabletop-go
Next up: learning + moving to layouts

erm3nda ler3nda

unread,
May 20, 2018, 9:27:37 AM5/20/18
to Kivy users support
https://kivy.org/docs/guide/kvlang.html

Pretty much outdated (dead) link.

ZenCODE

unread,
May 20, 2018, 10:02:06 AM5/20/18
to Kivy users support
Well, it was posted in 2012. 
Reply all
Reply to author
Forward
0 new messages