Skip to content

Instantly share code, notes, and snippets.

@Alan-FGR
Created July 6, 2016 19:43
Show Gist options
  • Save Alan-FGR/b3051e13d64d0faea7257540493f8c2b to your computer and use it in GitHub Desktop.
Save Alan-FGR/b3051e13d64d0faea7257540493f8c2b to your computer and use it in GitHub Desktop.
from kivy.app import App
from kivy.app import Builder
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
Builder.load_string("""
#:set RED (1, 0, 0, 1)
#:set GREEN (0, 1, 0, 1)
#:set BLUE (0, 0, 1, 1)
#:set YELLOW (1, 1, 0, 1)
<-Button>:
size_hint_y: None
height: 40
opacity: 0.8 if self.disabled else 1
color: RED if self.disabled else GREEN # doesn't work
# can't even use white and tint
# it on line 28 as a workaround :(
canvas.before:
Color:
rgb: YELLOW if self.disabled else BLUE
Rectangle:
size: self.size
pos: self.pos
canvas:
Color:
rgb: 1,1,1,1
Rectangle:
texture: self.texture
size: self.texture_size
pos: self.center_x - self.texture_size[0]*0.5, self.y+10
""")
class Test(App):
def build(self):
root_layout = StackLayout()
root_layout.padding = root_layout.spacing = 10
for i in xrange(6):
root_layout.add_widget(Button(text="DISABLED STYLING PROBLEM"))
root_layout.children[2].disabled=True
root_layout.children[5].disabled = True
return root_layout
def increase_size(self, *args):
self.my_unit += 1
print self.my_unit
if __name__ == "__main__":
Test().run()
@Alan-FGR
Copy link
Author

Alan-FGR commented Jul 6, 2016

OK, just found a workaround for that issue, basically setting disabled_color did the trick (undoc'd, had to browse the sources to find that out), but it doesn't take opacity into account so I've used list comprehension to workaround that problem in the original workaround. Yeah, a workaround in the workaround.
Here's the working version:

from kivy.app import App
from kivy.app import Builder

from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.uix.label import Label

Builder.load_string("""
#:set RED (1, 0, 0, 1)
#:set GREEN (0, 1, 0, 1)
#:set BLUE (0, 0, 1, 1)
#:set YELLOW (1, 1, 0, 1)

<-Label>:
    size_hint_y: None
    height: 40
    font_size: 34
    opacity: 0.8 if self.disabled else 1
    color: GREEN
    disabled_color: [i*self.opacity for i in RED]
    canvas.before:
        Color:
            rgb: YELLOW if self.disabled else BLUE
        Rectangle:
            size: self.size
            pos: self.pos
    canvas:
        Color:
            rgb: 1,1,1,1
        Rectangle:
            texture: self.texture
            size: self.texture_size
            pos: self.center_x - self.texture_size[0]*0.5, self.y
""")

class Test(App):
    def build(self):
        root_layout = StackLayout()
        root_layout.padding = root_layout.spacing = 10

        for i in xrange(5):
            root_layout.add_widget(Button(text="DISABLED STYLING PROBLEM"))

        for i in xrange(5):
            root_layout.add_widget(Label(text="DISABLED STYLING PROBLEM"))

        root_layout.children[2].disabled=True
        root_layout.children[5].disabled = True
        root_layout.children[8].disabled = True

        return root_layout

    def increase_size(self, *args):
        self.my_unit += 1
        print self.my_unit

if __name__ == "__main__":
    Test().run()```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment