Kivy: changing label background image

Viewed 42

I'm new to kivy, I'm trying to make a label either have an image as background or a background color, but im unable to make it work. ive written in the code comments what im trying to do.

I'm trying to make if color is selected the background image source is nothing, therefore removed. And when numbers is selected by the spinner the backgroundcolor is removed.

But when I have both a Color and a source in the kv file line 14-17, either the image or the background color doesnt work.

python file

from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout

class Test(BoxLayout):
    back_color = (1,0,0,0)
    numbers = False
    colors = False
background = "someimage.png"

def clrchooser(self, value):
    if value == "Color":
        self.color = True
        self.numbers = False
    elif value == "Numbers":
        self.numbers = True
        self.color = False
def test(self): 
    #if numbers is selected, the image should stay
    if self.numbers:
        self.ids.colorlabel.text = "1"
        print("numbers")
        self.back_color = ()

    #if numbers is selected, the background image should be removed and the color should change
    elif self.color:
        print("color")
        self.background = ""
        #self.ids.colorlabel.source ="" #doesnt work
        self.back_color = (1,1,1,1)

root = Builder.load_file("mainpage.kv")
class MainApp(App):
    def build(self):
    return root

if __name__ == '__main__':
    MainApp().run()  
    

mainpage.kv

Test:
    orientation:"vertical"
    Spinner:
    id:clrspinner
    text:"mode"
    values: ["Color", "Numbers"]
    on_text: root.clrchooser(clrspinner.text)
    halign:"center"

    Label:
        id:colorlabel
        canvas.before:
            #when color is added, the background image source is removed
            Color:
                rgba: root.back_color
            Rectangle:
                pos: self.pos
                size: self.size
                source: root.background
        text:"hello"
        Button:
            text:"test"
            on_press:root.test()

    
0 Answers
Related