Changing image in widget - Kivy - Python

2,518 views
Skip to first unread message

Shaurya Chaudhuri

unread,
Jan 30, 2014, 9:41:31 PM1/30/14
to kivy-...@googlegroups.com
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Rectangle
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image


class Imglayout(FloatLayout):


 
def __init__(self,**args):
 
super(Imglayout,self).__init__(**args)
 
 
with self.canvas.before:
 
Color(0,0,0,0)
 
self.rect=Rectangle(size=self.size,pos=self.pos)
 
 
self.bind(size=self.updates,pos=self.updates)
 
def updates(self,instance,value):
 
self.rect.size=instance.size
 
self.rect.pos=instance.pos
 


class MainTApp(App):
 
 im
=Image(source='img1.jpg')
 
def build(self):
 root
= BoxLayout(orientation='vertical')
 c
= Imglayout()
 root
.add_widget(c)
 
 
 
self.im.keep_ratio= False
 
self.im.allow_stretch = True
 cat
=Button(text="Categories",size_hint=(1,.07))
 cat
.bind(on_press=self.callback)
 c
.add_widget(self.im)
 root
.add_widget(cat);
 
return root
 
 
def callback(self,value):
 
self.im=Image(source='img2.jpg')


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


What i am trying to do here, is change the image first loaded during object creation, which is shown when the app starts, and then change it when the button cat is pressed. I am trying it to do this way but it isnt happening. I would eventually want it to change with a swipe gesture.(with a little bit of swipe animation like it happens in the phone

what i am trying to build is a slideshow, which will change image in t seconds, unless swiped, and then when a new image comes the timer resets. When the category button is pressed the image will not be there and a list of categories to select from. and when an item from the list is touched the images from that list will be displayed on the screen.

And at the end when everything has been done i would want to make it such that it automatically detects categories(based on directories in a specified location.) and then all the images will be available to it.(that is not telling it explicitly how many images and what images.)

But, i am not able to do the first thing, so i would really like some help on that. And maybe a few pointers on how to achieve the other things as well.

ZenCODE

unread,
Jan 31, 2014, 9:39:25 AM1/31/14
to kivy-...@googlegroups.com
You were close. You just need to access the instance and not create a new one...

from kivy.app import App

from kivy.uix.button import Button
from kivy.graphics import Color, Rectangle
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image


class Imglayout(FloatLayout):
   
def __init__(self, **args):
       
super(Imglayout, self).__init__(**args)
 
       
with self.canvas.before:
           
Color(0, 0, 0, 0)
           
self.rect = Rectangle(size=self.size, pos=self.pos)
 
       
self.bind(size=self.updates, pos=self.updates)

   
def updates(self,instance,value):
       
self.rect.size=instance.size
       
self.rect.pos=instance.pos


class MainTApp(App):

 
    im
= Image(source='img1.png')


   
def build(self):
        root
= BoxLayout(orientation='vertical')
        c
= Imglayout()
        root
.add_widget(c)

       
self.im.keep_ratio= False
       
self.im.allow_stretch = True
        cat
=Button(text="Categories", size_hint=(1,.07))
        cat
.bind(on_press=self.callback)
        c
.add_widget(self.im)
        root
.add_widget(cat);
       
return root
 
   
def callback(self, value):

       
self.im.source ='img2.png'

Luis Bill

unread,
Feb 20, 2014, 7:05:06 PM2/20/14
to kivy-...@googlegroups.com
Hi,

If you want to make this run automatically, how would you do that (for instance, using the Clock to schedule the change)?

Thaks

ZenCODE

unread,
Feb 21, 2014, 12:51:37 AM2/21/14
to kivy-...@googlegroups.com
Yes, a that would work. Depends on what you're trying to do...;-)

from kivy.clock import Clock
Clock.schedule_once(self.callback)


Reply all
Reply to author
Forward
0 new messages