【发布时间】:2021-05-18 14:25:09
【问题描述】:
我正在尝试在我的应用中添加一个屏幕来拍照,但在单击拍摄按钮时我不断收到此错误:AttributeError: 'AddPhotoScreen' object has no attribute 'capture'
这是我的 kivy 和 main.py 文件:
<AddPhotoScreen>:
name: "addphoto"
FloatLayout:
Camera:
id: camera
resolution: (1280, 800)
play: True
GridLayout:
cols: 1
rows: 2
spacing: 1
pos_hint: {"center_x":.5, "center_y":.1}
MDIconButton:
icon: "camera"
user_font_size: "64sp"
text_color: 0,0,0,0.9
pos_hint: {"center_x":.5, "center_y":.5}
on_press: root.capture()
MDIconButton:
icon: "keyboard-backspace"
user_font_size: "64sp"
text_color: 0,0,0,0.9
pos_hint: {"center_x":.5, "center_y":.5}
on_release:app.root.current = "nouvelele"```
Main.py file
class AddPhotoScreen(Screen):
def build(self):
return CameraClick()
class CameraClick(BoxLayout):
def capture(self):
camera = self.ids['camera']
timestr = time.strftime("%Y%m%d_%H%M%S")
camera.export_to_png("IMG_{}.png".format(timestr))
print ("Photo Added")
我猜这个问题是因为on_press: root.capture(),但我不明白,因为class AddPhotoScreen正在返回CameraClick。
此代码几乎完全是从 kivy 示例库中复制/粘贴的,因此它应该可以工作。
【问题讨论】:
标签: python class camera attributes kivy