【问题标题】:KIVY: 'float' has no attributes 'ids'KIVY: \'float\' 没有属性 \'ids\'
【发布时间】:2023-02-07 12:20:14
【问题描述】:

我正在尝试制作一个时钟,它只是当前时间每秒更新一次的标签。每次我尝试更新标签时都会抛出此错误:

 File "C:\Users\Nitro\Documents\MirOS\MirOS-core.py", line 33, in currentTime
 self.ids.current_time.text = timeData
 AttributeError: 'float' object has no attribute 'ids'

我对 kivy.Clock 函数做了一些研究,我发现这很可能发生,因为时钟函数调用 currentTime() 并包含一个增量时间参数,这是导致 AttributeError 的原因。不幸的是,我需要自己保持原样的论点,否则我的标签不会更新,我会抛出更多错误。

这是我的.py 文件:

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.clock import Clock 
from kivy.uix.screenmanager import ScreenManager, Screen, FallOutTransition
import time

sm = ScreenManager(transition = FallOutTransition())

Window.clearcolor = 0, 0, 0, 1
Window.size = 1920, 1080
Window.fullscreen = True

class StartUP(Screen):

    def SystemCheck(self):
        sm.current = 'active_home'
        print('WORKING')

    class StartUPCavas(Widget):
        pass

class ActiveHome(Screen):

    class ActiveHomeCanvas(Widget):
        pass

    class ActiveClock(Widget):

        def currentTime(self):
            timeData = time.strftime("%H:%M:%S")
            self.ids.current_time.text = timeData

        Clock.schedule_interval(currentTime, 1)


class MirOSApp(App):
    def build(self):
        sm.add_widget(StartUP(name = 'startup'))
        sm.add_widget(ActiveHome(name = 'active_home'))
        return sm

if __name__ == '__main__':
    MirOSApp().run()

这是.kv 文件:

#kivy 2.1.0

<StartUP>:

    StartUPCavas:

        Image:
            source: 'images/MirOS.png'
            texture: self.texture
            size_hint_y: None
            width: 300
            center_x: root.width / 2
            center_y: root.height / 2

        Button:
            center_x: root.width / 2
            center_y: (root.height / 2) - 100
            on_press:
                root.SystemCheck()

<ActiveHome>:

    ActiveHomeCanvas:

    ActiveClock:

        Label:

            id: current_time

            text: ''
            font_size: 40
            font_name: 'fonts/bahnschrift.ttf'
            center_x: root.width / 2
            center_y: root.height / 2
            color: 1, 1, 1, 1

我真的很困惑,并试图自己解决这个问题,但我似乎找不到任何解决方案。有任何想法吗?谢谢你的时间!

【问题讨论】:

    标签: python function object kivy clock


    【解决方案1】:

    您对增量时间参数 (dt) 的看法是正确的。该参数被传递给currentTime() 方法,但currentTime() 方法期望接收self。因此,当该方法执行 self.ids 时,它试图访问 dtids 属性,从而导致错误。

    为了提供 self 参数,您必须调用 currentTime() 方法作为 ActiveHome 的实例方法(因为这是定义 id 的地方)。

    您可以通过修改您的 ActiveHome 类来解决此问题,如下所示:

    class ActiveHome(Screen):
        def __init__(self, **kwargs):
            super(ActiveHome, self).__init__(**kwargs)
            Clock.schedule_interval(self.currentTime, 1)
    
        def currentTime(self, dt):
            timeData = time.strftime("%H:%M:%S")
            self.ids.current_time.text = timeData
    

    这种安排在 __init__() 方法中安排对 currentTime() 的调用,currentTime() 方法处理正确的参数。

    【讨论】:

      猜你喜欢
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 2018-06-02
      • 2020-03-25
      相关资源
      最近更新 更多