Skip to content

Instantly share code, notes, and snippets.

@kived
Last active March 1, 2022 20:25
Show Gist options
  • Save kived/862db38078170ec0ef83 to your computer and use it in GitHub Desktop.
Save kived/862db38078170ec0ef83 to your computer and use it in GitHub Desktop.
from kivy.app import App
from kivy.lang import Builder
root = Builder.load_string('''
<ScaleLabel@Label>:
_scale: 1. if self.texture_size[0] < self.width else float(self.width) / self.texture_size[0]
canvas.before:
PushMatrix
Scale:
origin: self.center
x: self._scale or 1.
y: self._scale or 1.
canvas.after:
PopMatrix
<-ScaleButton@Button>:
state_image: self.background_normal if self.state == 'normal' else self.background_down
disabled_image: self.background_disabled_normal if self.state == 'normal' else self.background_disabled_down
_scale: 1. if self.texture_size[0] < self.width else float(self.width) / self.texture_size[0]
canvas:
Color:
rgba: self.background_color
BorderImage:
border: self.border
pos: self.pos
size: self.size
source: self.disabled_image if self.disabled else self.state_image
PushMatrix
Scale:
origin: self.center
x: self._scale or 1.
y: self._scale or 1.
Color:
rgba: self.disabled_color if self.disabled else self.color
Rectangle:
texture: self.texture
size: self.texture_size
pos: int(self.center_x - self.texture_size[0] / 2.), int(self.center_y - self.texture_size[1] / 2.)
PopMatrix
BoxLayout:
orientation: 'vertical'
ScaleLabel:
size_hint_y: 0.2
text: 'my scale text here (%d, %.2f)' % (self.width, self._scale)
padding: dp(12), dp(12)
font_size: self.height * 0.5
ScaleButton:
size_hint_y: 0.2
text: 'scale button'
padding: dp(12), dp(12)
font_size: self.height * 0.5
Widget
''')
class TestApp(App):
def build(self):
return root
if __name__ == '__main__':
TestApp().run()
Copy link

ghost commented Nov 24, 2018

Thank you for this! I'm using the scaled label in my TeamMaker app. Considering to bring in the scaled button also when I redesign my interface. Good work!

@burcaka
Copy link

burcaka commented Feb 22, 2021

This is a gem. Thank you. Using it to auto change the font size of a calculation label.

A question, if I may: How can I right align the text? As it stands, text starts to flow from the center.

Tried:
text_size: self.size
halign: 'right'
Among other things, with no luck.

@jimbrayrcp
Copy link

👍

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