【发布时间】:2016-07-02 21:39:42
【问题描述】:
我试图弄清楚如何将使用 Kivy 语言布局的按钮绑定到函数。在用 Python 语言布置按钮时,我见过很多 answers。但是,一旦一切就绪并且您现在通过继承自 Button 的自定义类进行引用,该怎么办?
按下时,下面的代码会抛出错误TypeError: show() takes 1 positional argument but 2 were given 并使程序崩溃。
class TimerButton(ButtonBehavior, Image):
timer_container = ObjectProperty(None)
client_scoreboard = ObjectProperty(None)
def __init__(self, **kwargs):
super(TimerButton, self).__init__(**kwargs)
self.bind(on_press=self.show)
self.bind(on_release=self.changeImage)
def show(self):
print('hi there')
self.source = 'assets/mainViewTimerButtonPressed.png'
import kivy.animation as Animate
anim = Animate.Animation(
x=self.client_scoreboard.right - self.timer_container.width,
y=self.timer_container.y,
duration=0.25)
anim.start(self.timer_container)
self.unbind(on_press=self.show)
self.bind(on_press=self.hide)
def changeImage(self):
self.source = 'assets/mainViewTimerButton.png'
def hide(self):
import kivy.animation as Animate
anim = Animate.Animation(
x=self.client_scoreboard.width,
y=self.timer_container.y,
duration=0.25)
anim.start(self.timer_container)
self.unbind(on_press=self.hide)
self.bind(on_press=self.show)
【问题讨论】: