我修改了Kivy的图片演示示例。我将canvas.before更改为.kv文件中的canvas.after以隐藏带有白色封面的图片。我在.py文件中添加了一个按钮来清除canvas.after on_press以删除白色封面并显示我的照片。问题是我只能访问.kv文件中的根类,即FloatLayout:。我需要访问类规则:为了能够使用canvas.after.class()函数清除canvas.after。
这是.py文件:
#!/usr/bin/kivy
'''
Pictures demo
=============
This is a basic picture viewer, using the scatter widget.
'''
import kivy
kivy.require('1.0.6')
from glob import glob
from random import randint
from os.path import join, dirname
from kivy.app import App
from kivy.uix.button import Button
from kivy.logger import Logger
from kivy.uix.scatter import Scatter
from kivy.properties import StringProperty
# FIXME this shouldn't be necessary
from kivy.core.window import Window
class Picture(Scatter):
'''Picture is the class that will show the image with a white border and a
shadow. They are nothing here because almost everything is inside the
picture.kv. Check the rule named <Picture> inside the file, and you'll see
how the Picture() is really constructed and used.
The source property will be the filename to show.
'''
source = StringProperty(None)
class PicturesApp(App):
def build(self):
# the root is 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)
# get any files into images directory
curdir = dirname(__file__)
for filename in glob(join(curdir, 'images', '*')):
try:
# load the image
picture = Picture(source=filename, rotation=randint(-30, 30))
# add to the main field
root.add_widget(picture)
except Exception as e:
Logger.exception('Pictures: Unable to load <%s>' % filename)
def on_pause(self):
return True
if __name__ == '__main__':
PicturesApp().run()
这是.kv文件:
#: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 a picture is created, the image can delay the loading
# as soon as the image is loaded, ensure that the center is changed
# to the center of the screen.
on_size: self.center = win.Window.center
size: image.size
size_hint: None, None
Image:
id: image
source: root.source
# create initial image to be 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)
答案 0 :(得分:1)
根据您要清除画布的位置:在build()方法中,您可以执行
picture.ids.image.canvas.clear()
#or
picture.ids['image'].canvas.clear()
如果在Picture类中:
self.ids.image.canvas.clear()
或者,您可以在不清除的情况下进行更改,具体方法如下:
class Picture(Scatter):
visible = BooleanProperty(True)
在.kv中:
<Picture>:
...
Image:
...
canvas.after:
Color:
rgba: (1,1,1,1 if root.visible else 0)
...
或类似的东西。