【问题标题】:Changing button colour everytime button is pressed kivy每次按下按钮时更改按钮颜色 kivy
【发布时间】:2019-10-02 21:51:40
【问题描述】:

我希望我的按钮在按下时从蓝色变为绿色。然后,如果再次按下它,我希望它从绿色变回蓝色。我知道如何在按下按钮时更改按钮的颜色,但如果再次按下按钮,我不知道如何将该颜色更改回原始颜色。

kv 文件:

<Type>:
    name: "type"
        RoundedButton:
            size_hint: 0.417, 0.15625
            pos_hint: {"x": 0.0556, "y": 0.15}
            on_press: root.change_color()
            Image:
                source: 'Job.PNG'
                size: self.parent.width, .85 * self.parent.height
                pos: self.parent.x, self.parent.y + 5
                stretch: True
                keep_ratio: False

<RoundedButton@Button>:
    background_normal: ""
    background_color: 0, 0, 0, 0
    back_color: 0.2,0.6,1,1
    border_radius: 10
    color: self.back_color
    bold: True
    canvas.before:
        Color:
            rgba: self.back_color
        Line:
            rounded_rectangle: self.x, self.y, self.width, self.height, self.border_radius
            width: 

python 文件:

class Type(Screen):
    back_color = ObjectProperty()
    def change_color(self):
        if self.back_color == (0.2,0.6,1,1):
            self.back_color = (0, 1, 0, 1)
        else:
            self.back_color = (0.2,0.6,1,1)

我以为我在 py 文件中的逻辑会检查颜色是否为蓝色(0.2,0.6,1,1),即没有按下,它会变成绿色,如果不是蓝色(所以必须是绿色,它会变成蓝色。但是当我按下按钮时没有任何反应,没有错误,它只是没有变成绿色。

【问题讨论】:

    标签: python python-3.x kivy kivy-language


    【解决方案1】:

    几个问题。

    首先,您的change_color() 方法试图更改Type 实例中的back_color,但您的RoundedButton 规则引用了RoundedButton 实例中的back_color(不是@ 987654327@)。因此,该方法所做的任何更改都不会影响RoundedButton

    二、if声明:

    if self.back_color == (0.2,0.6,1,1):
    

    永远不会是真的。部分是因为第一个问题,还需要比较:

    if self.back_color == [0.2,0.6,1,1]:
    

    我不知道为什么会这样。

    我认为解决第一个问题的最简单方法是将id 添加到RoundedButton

    <Type>:
        name: "type"
        RoundedButton:
            id: butt
            size_hint: 0.417, 0.15625
            pos_hint: {"x": 0.0556, "y": 0.15}
            on_press: root.change_color()
    

    然后修改change_color()方法为:

    class Type(Screen):
        def change_color(self):
            if self.ids.butt.back_color == [0.2,0.6,1,1]:
                self.ids.butt.back_color = (0, 1, 0, 1)
            else:
                self.ids.butt.back_color = (0.2,0.6,1,1)
    

    请注意,back_color ObjectProperty 不是必需的。

    如果您希望RoundedButtons 独立更改颜色,您可以将RoundedButton 定义为:

    class RoundedButton(Button):
        back_color = ListProperty()
    
        def change_color(self):
            if self.back_color == [0.2,0.6,1,1]:
                self.back_color = [0, 1, 0, 1]
            else:
                self.back_color = [0.2,0.6,1,1]
    

    在您的kv 文件中:

    <Type>:
        name: "type"
        RoundedButton:
            size_hint: 0.417, 0.15625
            pos_hint: {"x": 0.0556, "y": 0.15}
            Image:
                source: 'Job.PNG'
                size: self.parent.width, .85 * self.parent.height
                pos: self.parent.x, self.parent.y + 5
                stretch: True
                keep_ratio: False
        RoundedButton:
            size_hint: 0.417, 0.15625
            pos_hint: {"right": 1, "top": 1}
            Image:
                source: 'Job.PNG'
                size: self.parent.width, .85 * self.parent.height
                pos: self.parent.x, self.parent.y + 5
                stretch: True
                keep_ratio: False
    
    <RoundedButton>:
        background_normal: ""
        background_color: 0, 0, 0, 0
        back_color: 0.2,0.6,1,1
        border_radius: 10
        color: self.back_color
        bold: True
        on_press: self.change_color()
        canvas.before:
            Color:
                rgba: self.back_color
            Line:
                rounded_rectangle: self.x, self.y, self.width, self.height, self.border_radius
    

    【讨论】:

    • 谢谢,这很好用。我在同一个屏幕上有多个 RoundedButtons,我想要完全相同的效果。我是否需要将 Id:butt 提供给他们从 (RoundedButton@Button) 继承的 Button 而不是我在上面完成的单个按钮?我可以测试一下,但现在几天都不会在我的电脑前,很想知道
    • @John Anderson 比较不起作用,因为 Kivy 语言中的颜色是 ListProperty 。在这里if self.back_color == (0.2,0.6,1,1) 你检查一个列表== 元组是否总是返回假。同样在您的作业中,您还应该使用列表而不是元组。它正在工作,但以后可能会导致问题。
    • 您不能将id 放在&lt;RoundedButton@Button&gt; 中,任何id 都会添加到规则根目录中的字典中,在这种情况下,RoundedButton 是该规则的根规则。此外,您只能拥有一个具有特定id 的对象。如果您希望对多个Buttons(独立更改颜色)具有此效果,则应定义自己的RoundedButton 子类,该子类具有back_color 属性和change_color() 方法。那么你的on_press 会更像on_press: self.change_color()。并对change_color()进行了一些适当的更改。
    • 我已添加到我的原始答案中以证明我的上述评论。
    • 感谢你们的帮助,我现在明白了,可以看到我如何设置一个链接到我的 python 逻辑的按下事件并将其应用于该按钮/小部件的所有实例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2019-10-13
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    相关资源
    最近更新 更多