from kivy.uix.button import Button
from kivy.properties import ListProperty
from kivy.properties import NumericProperty
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.app import App
class CustomButton(Button):
# defaults
border_color = ListProperty([1, 1, 1, 1])
corner_radius = NumericProperty(0)
bg_color = ListProperty([0, 0, 0, 1])
border_width = NumericProperty(0)
Builder.load_string('''
<CustomButton@Button>:
# should I define border_color, corner_radius, bg_color and border_width
# here or within my python script like I did here?
background_color: 0, 0, 0, 0
background_normal: ''
background_down: ''
canvas.before:
Color:
rgba: self.border_color
RoundedRectangle:
size: self.size
pos: self.pos
radius: [self.corner_radius]
Color:
rgba: self.bg_color
RoundedRectangle:
size: [i - 2 * self.border_width for i in self.size]
pos: [i + self.border_width for i in self.pos]
radius: [self.corner_radius - self.border_width if self.corner_radius != 0 else 0]
''')
class RootWidget(FloatLayout):
pass
Builder.load_string('''
<RootWidget>:
CustomButton:
size_hint: 0.2, 0.1
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
text: 'HELLO'
border_width: 0.1 * self.height
on_press:
self.border_width = 0.2 * self.height
''')
class TestApp(App):
def build(self):
return RootWidget()
TestApp().run()
Here is what I changed.
from kivy.uix.button import Button
from kivy.properties import ListProperty
from kivy.properties import NumericProperty
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.app import App
class CustomButton(Button):
# defaults
border_color = ListProperty([1, 1, 1, 1])
corner_radius = NumericProperty(0)
bg_color = ListProperty([0, 0, 0, 1])
border_width = NumericProperty(0)
Builder.load_string(
'''
<CustomButton>:
# should I define border_color, corner_radius, bg_color and border_width
# here or within my python script like I did here?
background_color: 0, 0, 0, 0
background_normal: ''
background_down: ''
canvas.before:
Color:
rgba: self.border_color
RoundedRectangle:
size: self.size
pos: self.pos
radius: [self.corner_radius]
Color:
rgba: self.bg_color
RoundedRectangle:
size: [i - 2 * self.border_width for i in self.size]
pos: [i + self.border_width for i in self.pos]
radius: [self.corner_radius - self.border_width if self.corner_radius != 0 else 0]
border_width: 0.1 * self.height
on_press: self.border_width = 0.2 * self.height
on_release: self.border_width = 0.1 * self.height
''')
class RootWidget(FloatLayout):
pass
Builder.load_string('''
<RootWidget>:
CustomButton:
size_hint: 0.2, 0.1
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
text: 'HELLO'
on_press: print('Button Pressed')
on_release: print('Button Released')
''')
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/90de4622-749f-4324-ae75-53e1a4e9bb9eo%40googlegroups.com.