![](/img/trans.png)
[英]How do I fix 'ValueError: callback must be a callable, got 18:19:46'
[英]How do I fix ' ValueError: callback must be a callable, got None' on schedule Kivy.clock callback function?
我有一個使用plyer.facades.Wifi
庫檢查wifi狀態的函數。 該函數根據wifi的狀態將BooleanProperty
變量is_wifi
更改為True
或False
。 BooleanProperty
變量在Kv-Language
腳本中綁定到ActionLabel
,后者根據狀態更改圖像。 然后使用Kivy的Clock.schedule_interval()
調度該函數。
主要問題是我收到ValueError: callback must be a callable, got None
在安排函數回調時ValueError: callback must be a callable, got None
。
我嘗試過:1]在初始化時調度函數。 2]初始化后,當用戶登錄時調用調度事件。
from plyer import wifi
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
class TheLogger(FloatLayout):
is_wifi = BooleanProperty(wifi.is_enabled())
def __init__(self, **kwargs):
super().__init__(**kwargs)
def wifi_is_enabled(self): #Scheduling a callback of this function
print('checking connection')
try:
if wifi.is_enabled():
self.is_wifi = True
else:
self.is_wifi = False
except OSError:
pass
class LoginApp(App):
title = 'Login'
def build(self):
self.icon = 'sign_in_5243564.png'
rt = TheLogger()
Clock.schedule_interval(rt.wifi_is_enabled(), 0.5) #scheduling callback of wifi_is_enabled() function
return rt
Builder.load_string('''
<TheLogger>:
un_input: user_in
ScreenManager:
id: _screen_manager
Screen:
name: 'choice'
ActionBar:
pos_hint: {'top': 1, 'right': 1}
canvas:
Color:
rgba: (0,0.4,0.51,1)
Rectangle:
pos: self.pos
size: self.size
ActionView:
use_separator: True
ActionPrevious:
title: "Sign Out"
with_previous: True
app_icon: ''
color: (1,1,1,1)
on_release: app.root.sign_out()
ActionLabel: #ActionLabel source with If else block code on callback
text: ''
Image:
source: 'green_wifi_connected_5456.png' if root.is_wifi else 'red_ic_signal_wifi_off_48px_352130.png'
center_y: self.parent.center_y
center_x: self.parent.center_x
size: self.parent.width /1, self.parent.height/ 1
allow_stretch: True
''')
我希望函數安排沒有錯誤的回調。
Clock.schedule_interval(rt.wifi_is_enabled(), 0.5)
此代碼等效於:
callback = rt.wifi_is_enabled()
Clock.schedule_interval(callback, 0.5)
您現在看到問題了嗎? callback的值為None,這是您嘗試安排的時間。
您需要計划函數本身,而不是其返回值:
Clock.schedule_interval(rt.wifi_is_enabled, 0.5)
請注意,該函數將自動接收一個位置自變量,其中包含自上次運行/調度以來的時間。 您的函數將需要接受此參數,即使它忽略了它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.