python - Kivy demo pictures - I am trying to modify the Class Rule <Picture> and clear the canvas.after -


i modified pictures demo example kivy. changed canvas.before canvas.after in .kv file hide pictures white cover. added button in .py file clear canvas.after on_press remove white cover , show pictures. problem can access root class in .kv file floatlayout:. need access class rule : in order able clear canvas.after using canvas.after.class() function.

here .py file:

    #!/usr/bin/kivy ''' pictures demo =============  basic picture viewer, using scatter widget. '''  import kivy kivy.require('1.0.6')  glob import glob random import randint os.path import join, dirname kivy.app import app kivy.uix.button import button kivy.logger import logger kivy.uix.scatter import scatter kivy.properties import stringproperty # fixme shouldn't necessary kivy.core.window import window   class picture(scatter):     '''picture class show image white border ,     shadow. nothing here because inside     picture.kv. check rule named <picture> inside file, , you'll see     how picture() constructed , used.      source property filename show.     '''      source = stringproperty(none)   class picturesapp(app):      def build(self):          # root created in pictures.kv         root = self.root          btn1 = button(text='show pictures', size=(100, 50), size_hint=(none, none), pos=(600, 50))         btn1.bind(on_press=root.canvas.after.clear())         root.add_widget(btn1)          # files images directory         curdir = dirname(__file__)         filename in glob(join(curdir, 'images', '*')):             try:                 # load image                 picture = picture(source=filename, rotation=randint(-30, 30))                 # add main field                 root.add_widget(picture)              except exception e:                 logger.exception('pictures: unable load <%s>' % filename)      def on_pause(self):         return true   if __name__ == '__main__':     picturesapp().run() 

here .kv file:

#:kivy 1.0 #:import kivy kivy #:import win kivy.core.window  floatlayout:     canvas:         color:             rgb: 1, 1, 1         rectangle:             source: 'data/images/background.jpg'             size: self.size      boxlayout:         padding: 10         spacing: 10         size_hint: 1, none         pos_hint: {'top': 1}         height: 44         image:             size_hint: none, none             size: 24, 24             source: 'data/logo/kivy-icon-24.png'         label:             height: 24             text_size: self.width, none             color: (1, 1, 1, .8)             text: 'kivy %s - pictures' % kivy.__version__    <picture>:     # each time picture created, image can delay loading     # image loaded, ensure center changed     # center of screen.     on_size: self.center = win.window.center     size: image.size     size_hint: none, none      image:         id: image         source: root.source          # create initial image 400 pixels width         size: 80, 80 / self.image_ratio          # add shadow background         canvas.after:             color:                 rgba: 1,1,1,1             borderimage:                 source: 'shadow32.png'                 border: (36,36,36,36)                 size:(self.width+72, self.height+72)                 pos: (-36,-36) 

depending want clear canvas: in build() method, can do

picture.ids.image.canvas.clear() #or picture.ids['image'].canvas.clear() 

if within picture class:

self.ids.image.canvas.clear() 

alternatively, can change without clearing, here's how:

class picture(scatter):     visible = booleanproperty(true) 

in .kv:

<picture>: ...      image:       ...         canvas.after:             color:                 rgba: (1,1,1,1 if root.visible else 0) ... 

or that.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -