我试着做一个滑块使用一个按钮,当按下显示另外两个按钮,但不改变颜色时,按下仅仅是为了美观。我对.kv语言很熟悉,并且知道你可以使用
background_color: (1,0,0,1) if self.state == 'normal' else (0,1,0,1)
在脚本的.kv部分中,并尝试了这种方法的变体,并使用了if self.state == 'down':和其他类似于python的方法。然而,我现在正在尝试学习kivy的动态方面,并且刚刚开始使用动画,并且希望能够做到这一点而不用使用.kv文件或生成器。
目标是有两个按钮,在一个简单的幻灯片on_press按钮下改变背景色。我的问题是,当我希望背景色保持静态时,滑动按钮正在改变颜色,如果我调整背景色,下面的按钮不会突出显示on_press。我主要是寻求帮助与滑动按钮保留一种颜色,不太关心,如果下方的按钮必须保持默认。如果有人能提供帮助,我会非常感激的。
from kivy.animation import Animation
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.behaviors import ButtonBehavior
global active
active = True
class TestApp(App):
def plop_slide_animation(self, instance):
global active
if active == True:
plop_slide = Animation(pos=(250, 350), duration=0.5)
active = False
elif active == False:
plop_slide = Animation(pos=(250, 200), duration=0.5)
active = True
plop_slide.start(instance)
def ploppi_press(self,instance):
print('ploppi')
def plopper_press(self,instance):
print('plopper')
def build(self):
button1 = Button(background_color = (1,.6,1,1), size_hint=(.2, .2), pos=(250, 200), text='PLOP', on_press=self.plop_slide_animation)
button3 = Button(background_color = (128,0,0,.5), size_hint=(.09, .09), pos=(260, 250), text='ploppi', on_press=self.ploppi_press) #background_color = (128,0,0,.5),
button4 = Button(background_color = (0,0,255,.5), size_hint=(.09, .09), pos=(450, 250), text='plopper', on_press=self.plopper_press) #background_color = (0,0,255,.5),
layout = FloatLayout()
def change_text(button):
global active
if button1.state == 'down':
print(button1.state)
if active == True:
button1.text = 'PLOOOOP'
print('PLOOOOP')
if active == False:
button1.text = 'PLOP'
print('PLOP')
button1.bind(on_press=change_text)
layout.add_widget(button3)
layout.add_widget(button4)
layout.add_widget(button1)
return layout
if __name__ == '__main__':
TestApp().run()```发布于 2019-08-08 00:23:17
在不同状态下将空字符串释放到按钮的图像中
button1.background_down = ''
button1.background_normal = ''你总能得到一种颜色。
但这将是很少不同的颜色,因为它将显示真实的颜色,而不是暗淡的颜色。
医生:按钮
编辑:--您也可以将一个图像分配给另一个图像,它将对这两种状态使用相同的图像--并且得到相同的暗淡颜色。
button1.background_down = button1.background_normalhttps://stackoverflow.com/questions/57403606
复制相似问题