I'm building an app in python kivy.
I've got a switch widget ( switch1
), and when it's active I want to print ' Hello world' every 30 seconds .
In my attempt below I first define a callback ( switch1_callback
) that prints ' Hello world '. Then I define another callback ( timely_switch1_callback
) with Clock.schedule_interval()
that calls switch1_callback
. timely_switch1_callback
is bound to switch1
.
I keep getting an error message (TypeError: switch1_callback() takes 1 positional argument but 2 were given)
.
Thanks for any help!
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Rectangle, Color
from kivy.clock import Clock
kv = '''
<PrettySwitch>:
size_hint: None, None
size: 300, 300
canvas:
Color:
rgba: 0, 0, 1, 1
Rectangle:
size: self.size
pos: self.pos
Switch:
id: switch1
pos: 150, 150
size_hint: None, None
size: 100, 50
active: True
on_active: root.timely_switch1_callback()
'''
class PrettySwitch(FloatLayout):
def switch1_callback(self):
print('Hello world')
def timely_switch1_callback(self):
Clock.schedule_interval(self.switch1_callback, 30)
class SwitchApp(App):
def build(self):
Builder.load_string(kv)
return PrettySwitch()
if __name__ == '__main__':
SwitchApp().run()
Just change your definition of switch1_callback()
to take 2 arguments:
def switch1_callback(self, dt):
The dt
argument is added by Clock.schedule_interval()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.