AttributeError:调用Clock.schedule_interval函数时,'NoneType'对象在Kivy中没有属性'ids'

时间:2020-07-16 20:03:36

标签: python python-3.x kivy kivy-language attributeerror

我在下面的代码中遇到属性错误。我是这个奇特的框架的新手。当我尝试调用Clock.schedule_interval函数无限滚动图像时。 Clock.schedule_interval(self.root.ids.mainwindow.scroll_textures,1/60。) AttributeError:'NoneType'对象没有属性'ids' 出现此错误。 .py文件


import kivy
from kivy.app import App
from kivy.lang import Builder

from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.image import Image
from kivy.core.window import Window
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.core.audio import SoundLoader
from kivy.config import Config

# You can create your kv code in the Python file

# Create a class for all screens in which you can include
# helpful methods specific to that screen
kv = Builder.load_file("main.kv")

class MainWindow(Screen):
    cloud_texture = ObjectProperty(None)
    floor_texture = ObjectProperty(None)
    sound = ObjectProperty(None)

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.cloud_texture = Image(source = "cloud4.png").texture
        self.cloud_texture.wrap = 'repeat'
        self.cloud_texture.uvsize = (Window.width/self.cloud_texture.width,-1)
        self.floor_texture = Image(source = "floor2.jpg").texture
        self.floor_texture.wrap = 'repeat'
        self.floor_texture.uvsize = (Window.width/self.floor_texture.width,-1)
        self.sun_texture = Image(source = "sun.png").texture
        self.sun_texture.uvsize = (Window.width/self.sun_texture.width,-1)
        self.sound = SoundLoader.load('8bitmenu.wav')
        self.sound.play()

    def scroll_textures(self, time_passed):
        #Update the uvpos
        self.cloud_texture.uvpos = ((self.cloud_texture.uvpos[0] - time_passed/20) % Window.width, self.cloud_texture.uvpos[1])
        self.floor_texture.uvpos = ((self.floor_texture.uvpos[0] + time_passed/8) % Window.width, self.floor_texture.uvpos[1])
        #Redraw textures
        texture = self.property('cloud_texture')
        texture.dispatch(self)

        texture = self.property('floor_texture')
        texture.dispatch(self)

class Window1(Screen):
    pass

class WindowManager(ScreenManager):
    pass


#Config.write()
class MyApp(App):
    def on_start(self):
        Clock.schedule_interval(self.root.ids.mainwindow.scroll_textures, 1/60.)
        pass
    def build(self):
        return kv


MyApp().run()

.kv文件

<WindowManager>:
    MainWindow:
    Window1:


<MainWindow>:
    name: "main"
    id: mainwindow
    canvas.before:
        Rectangle:
            size: self.size
            pos: self.pos
            source: "sky.jpg"
        Rectangle:
            size: self.width - 1000, 120
            pos: self.pos[0] + 500, self.pos[1] + self.height - 138
            texture: self.sun_texture
        Rectangle:
            size: self.width, 138
            pos: self.pos[0], self.pos[1] + self.height - 168
            texture: self.cloud_texture
        Rectangle:
            size: self.width, 100
            pos: self.pos[0], self.pos[1]
            texture: self.floor_texture

    Image:
        source: 'dog2.gif'
        anim_delay: 0.09
        size: self.width, self.height
        pos: -270, -270

    Image:
        source: 'boy1.gif'
        anim_delay: 0.09
        size: self.width, self.height
        pos: -100, -230

    Button:
        text: "Start your journey"
        color: .8,.9,0,1
        background_color: 1, 1, 1, 0.3
        halign: 'center'
        center: root.center
        font_size: 12
        size: 32, 32
        size_hint: .2, .2
        pos: 300, 250
        on_release:
            app.root.current = "win1"



<Window1>:
    name: "win1"
    Button:
        text: "Go back"
        on_release:
            app.root.current = "main"

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

我在这里解决了错误。刚刚在父类中声明了ID,并且有效

WindowManager:
    MainWindow:
        id: mainwindow
    Window1: