How do I fix ' ValueError: callback must be a callable, got None' on schedule Kivy.clock callback function?
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
。
我试过:
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
在 ActionLabel 上显示绑定的 Kivy 语言示例
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,这是您尝试安排的值。
您需要安排函数本身,而不是它的 return 值:
Clock.schedule_interval(rt.wifi_is_enabled, 0.5)
请注意,该函数将自动接收一个位置参数,其中包含自上次 run/scheduled 以来的时间。您的函数将需要接受此参数,即使它会忽略它。
我有一个使用 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
。
我试过: 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
在 ActionLabel 上显示绑定的 Kivy 语言示例
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,这是您尝试安排的值。
您需要安排函数本身,而不是它的 return 值:
Clock.schedule_interval(rt.wifi_is_enabled, 0.5)
请注意,该函数将自动接收一个位置参数,其中包含自上次 run/scheduled 以来的时间。您的函数将需要接受此参数,即使它会忽略它。