简体   繁体   中英

having trouble changing the color of a button in Kivy

I am new at kivy/pyhton (and programming in general). I'm trying to create a mastermind-like game. So basically you press 4 buttons trying to guess the correct color combination and if you win, a message pops up saying that you won.

But the thing that I'm struggling with is the fact that I want the user to be able to press the same button multiple times and that each time the button is clicked, it's color changes.

So far I've come up with this (kv file):

    Button:
        id: b11
        pos_hint: {"x": 0.36, "y": 0.70}
        size_hint: 0.065, 0.065
        
        background_normal: ""
        background_color: (0,0,0,0)
        on_release: root.btn_action()

        canvas.before:
            Color:
                rgba: (1,1,1,0.2)
            Ellipse:
                size: self.size
                pos: self.pos
                size: (45, 45)

And this (py file):

def btn_action(self):
    global rgba = []
    red = [1, 0, 0, 1]  
    green = [0, 1, 0, 1]  
    blue = [0, 0, 1, 1]  
    purple = [1, 0, 1, 1]
    grey = [1,1,1,0.2]
    if rgba == grey:
        rgba = red

When I run the code, it tells me that rgba is not defined (which btw I do not know how to define) and thus gives me a syntax error.

I've also tried to work with the button's id, it didn't crash the programm but it also didn't do anything:

py file

def btn_action(self):
    b11 = ListProperty("")
    self.b11 = (1,0,0,1)

It's probably a stupid mistake and I'm sorry for my newbie-ness, but any help is welcome!

After relearning some kivy and help from creative googling, I created this fully unoptimized example of how you could do what you are attempting:

kivy_button_click.kv:

MyButton:
    id: b11
    pos_hint: {"x": 0.36, "y": 0.70}
    size_hint: 0.065, 0.065

    background_normal: ""
    background_color: (0,0,0,0)
    on_release: root.btn_action()

    canvas.before:
        Color:
            rgba: self.rgba
        Ellipse:
            size: self.size
            pos: self.pos
            size: (45, 45)

And Kivy_Button_Click.py:

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.properties import ListProperty

red = [1, 0, 0, 1]
green = [0, 1, 0, 1]
blue = [0, 0, 1, 1]
purple = [1, 0, 1, 1]
grey = [1,1,1,0.2]

class MyButton(Button):
    rgba = ListProperty()

    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        self.rgba = grey

    def btn_action(self):
        if self.rgba == grey:
            self.rgba = red
        elif self.rgba == red:
            self.rgba = green
        elif self.rgba == green:
            self.rgba = purple
        elif self.rgba == purple:
            self.rgba = blue
        else:
            self.rgba = grey

class Kivy_Button_ClickApp(App):
    pass

if __name__ == "__main__":
    Kivy_Button_ClickApp().run()

This should at least get you a little closer. I don't know what you'll need to do for multiple buttons.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM