我正在寫一個應用程序,使用kivy來檢查USB設備是否連接,而無需重新啓動我的應用程序,根據我使用Clock的文檔經常在特定時間間隔內調用所需的方法。但是,這總是說以下錯誤:如何使用kivy中的Clock.schedule_interval更新按鈕的文本?
from __future__ import print_function
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.clock import Clock
import wmi
kv = """<[email protected]>:
orientation: 'horizontal'
name: 'introscreen'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'index.png'
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
BoxLayout:
orientation:'horizontal'
size_hint: .5, .1
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
spacing: 20
pos_hint: {'center_x':.8, 'center_y': .8}
AnchorLayout:
anchor_x: 'left'
size_hint_x: .5
Button:
id: statebtn
size_hint: None, None
height: 50
width: self.texture_size[0]
padding: 10, 10
text: app.getusbdevices()"""
Builder.load_string(kv)
statebtn = Button(text="INSTRUMENT_NOT_FOUND")
class KartScan(FloatLayout):
def __init__(self, **kwargs):
super(KartScan, self).__init__(**kwargs)
class KartScanApp(App):
def build(self):
k = KartScan()
Clock.schedule_interval(self.getusbdevices, 2)
return k
def getusbdevices(self):
c = wmi.WMI()
statebtn.text = u"INSTRUMENT_NOT_FOUND"
wql = "Select * From Win32_USBControllerDevice"
for item in c.query(wql):
if "USB Device" in item.Dependent.Caption:
statebtn.text = u"CONNECTED"
else:
statebtn.text = u"INSTRUMENT_NOT_FOUND"
return statebtn.text
if __name__ == '__main__':
KartScanApp().run()
以上是我的代碼:有人可以幫我解決這個問題嗎? 這是我得到
kivy._clock.CyClockBase._process_events (kivy_clock.c:7700) File "kivy_clock.pyx", line 397, in kivy._clock.CyClockBase._process_events (kivy_clock.c:7577) File "kivy_clock.pyx", line 395, in kivy._clock.CyClockBase._process_events (kivy_clock.c:7498) File "kivy_clock.pyx", line 167, in kivy._clock.ClockEvent.tick (kivy_clock.c:3490) TypeError: getusbdevices() takes exactly 1 argument (2 given)
這是個日誌我得到 kivy._clock.CyClockBase._process_events(kivy \ _clock.c:7700) 文件 「kivy \ _clock.pyx」,線路397,在kivy._clock.CyClockBase._process_events( kivy \ _clock.c:7577) kivy._clock.CyClockBase._process_events(kivy \ _clock.c:7498)中的文件「kivy \ _clock.pyx」,第395行, 文件「kivy \ _clock.pyx」,第167行,在kivy._clock.ClockEvent.tick(kivy \ _clock.c:3490) TypeError:getusbdevices()只需要1個參數(給出2個) –