【问题标题】:Kivy AttributeError: object has no attributeKivy AttributeError:对象没有属性
【发布时间】: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


    【解决方案1】:

    错误信息清楚地说明了错误。您的 AddPhotoScreen 没有名为 capture 的方法。从其他方法返回对这一事实没有影响。你需要在AddPhotoScreen中实现capture()方法。

    【讨论】:

      猜你喜欢
      • 2021-10-16
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 2020-11-28
      • 2016-08-09
      • 2020-09-03
      相关资源
      最近更新 更多