简体   繁体   中英

pyhon Kivy change button color on press

I have Kivy on my RPi4 in Python 3. I created kind of a matrix design with 600pixels plus a few settingsbuttons. The buttons have all a different name and id. I colored them black at the programstart. In the settings I can mix a random color with a red a green and a blue slider. If I push a matrix button after selecting the color the button should change its background to that color. And that's the problem. I tried several things to change it: I tried to call a function from the kv file to change it like this: self.background_color=(1.0, 0.0, 0.0, 1.0) but dosen't work nor error no change. I tried it directly in the kv file: on_press:self.background_color=color xy or something like that. there is only one line I know that works partly:

class sletrix(App):
    def build(self):
        return Builder.load_string(kv)
    def on_start(self):
        Clock.schedule_once(usbconnect, 5.0)
        Clock.schedule_interval(self.switch, 0.05)
    def switch(self,dt):
        self.root.ids.buttid.background_color = 10,0,0,1 

I mean the line in switch it works perfect. But i want the buttid to be a variable and not the real id from the button.I have no solution anymore. And I don't want to create 600 of those lines and more to bring this to an end. Sorry for my bad english, if it's hard to read. Thank you and I hope you can help me.

Try this. kivy code:

Button:
    id: button_one
    text: "Hello World"
    on_press: root.new()

Python code:

class Highest(Screen):
    def new(self):
        self.ids['button_one'].background_color = 1, 0, 1, 1

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