它将在canvas.before编写的行上引发Parser Exception:
kivy.lang.parser.ParserException: Parser: File "", line 21:
I python : ...
I python : 19: height: '48dp'
I python : 20: on_press: root.capture()
I python : >> 21: canvas.before:
I python : 22: PushMatrix
I python : 23: Rotate:
I python : ...
I python : Invalid class name
考虑到canvas.before和canvas.after只能在其他对象中调用,以下代码对我有用:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import time
Builder.load_string('''
:
orientation: 'vertical'
Camera:
id: camera
resolution: (640, 480)
play: False
canvas.before:
PushMatrix
Rotate:
angle: -90
origin: self.center
canvas.after:
PopMatrix
ToggleButton:
text: 'Play'
on_press: camera.play = not camera.play
size_hint_y: None
height: '48dp'
Button:
text: 'Capture'
size_hint_y: None
height: '48dp'
on_press: root.capture()
''')
class CameraClick(BoxLayout):
def capture(self):
'''
Function to capture the images and give them the names
according to their captured time and date.
'''
camera = self.ids['camera']
timestr = time.strftime("%Y%m%d_%H%M%S")
camera.export_to_png("IMG_{}.png".format(timestr))
print("Captured")
class TestCamera(App):
def build(self):
return CameraClick()
TestCamera().run()
资源:
https://kivy.org/doc/stable/examples/gen__canvas__rotation__py.html
https://github.com/kivy-garden/garden.zbarcam/blob/20171220/zbarcam/zbarcam.kv#L22