【问题标题】:Prevent animated button from changing color on_press or on_release (No kv / Python portion)防止动画按钮在 on_press 或 on_release 上改变颜色(无 kv / Python 部分)
【发布时间】:2019-12-15 15:13:18
【问题描述】:

我正在尝试使用一个按钮制作一个滑块,按下时会显示另外两个按钮,但仅出于美观而按下时不会改变颜色。我对 .kv 语言非常熟悉,并且知道您可以使用
background_color: (1,0,0,1) if self.state == 'normal' else (0,1,0,1)
在脚本的 .kv 部分中,并尝试了此方法的变体,并使用if self.state == 'down': 和其他方法,例如 python 中的方法。但是我现在正在尝试学习 kivy 的动态方面,并且刚刚开始玩动画,并且希望能够在不使用 .kv 文件或 Builder 的情况下做到这一点。

我们的目标是在一个简单地滑动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()```

【问题讨论】:

    标签: python animation button kivy background-color


    【解决方案1】:

    将空字符串分配给不同状态下的按钮图像

        button1.background_down = ''
        button1.background_normal = ''
    

    你总是得到一种颜色。

    但它的颜色会略有不同,因为它会显示真实的颜色,而不是暗淡的颜色。

    文档:Button


    编辑:您还可以将一张图片分配给另一张图片,它会在两种状态下使用相同的图片 - 您会得到相同的暗色。

        button1.background_down = button1.background_normal
    

    【讨论】:

      猜你喜欢
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多