【发布时间】: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