Hme*_*006 1 python python-3.x kivy kivy-language
我有一个使用plyer.facades.Wifi库来检查 wifi 状态的函数。该函数将BooleanProperty变量更改is_wifi为True或False取决于 wifi 的状态。该BooleanProperty变量在Kv-Language脚本中绑定到ActionLabel根据状态更改图像的an 。然后使用 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
Run Code Online (Sandbox Code Playgroud)
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
''')
Run Code Online (Sandbox Code Playgroud)
我希望该函数可以无错误地安排回调。
Clock.schedule_interval(rt.wifi_is_enabled(), 0.5)
Run Code Online (Sandbox Code Playgroud)
此代码等效于:
callback = rt.wifi_is_enabled()
Clock.schedule_interval(callback, 0.5)
Run Code Online (Sandbox Code Playgroud)
你现在看到问题了吗?callback 的值为 None,这是您尝试安排的。
您需要调度函数本身,而不是它的返回值:
Clock.schedule_interval(rt.wifi_is_enabled, 0.5)
Run Code Online (Sandbox Code Playgroud)
请注意,该函数将自动接收一个位置参数,其中包含自上次运行/调度以来的时间。你的函数需要接受这个参数,即使它忽略它。
| 归档时间: |
|
| 查看次数: |
3134 次 |
| 最近记录: |