Skip to content

Instantly share code, notes, and snippets.

@kived
Created March 30, 2015 18:49
Show Gist options
  • Save kived/93e8242e0c8ae8a80d21 to your computer and use it in GitHub Desktop.
Save kived/93e8242e0c8ae8a80d21 to your computer and use it in GitHub Desktop.
Kivy: Label use maximum font_size
import kivy
kivy.require('1.8.1')
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
root = Builder.load_string('''
Label:
text: 'hello'
''')
class TestApp(App):
def build(self):
self.trigger_update_size = Clock.create_trigger(self.update_size)
root.bind(size=self.trigger_update_size)
# this makes sure the callback is run once when the texture is first created
root.bind(texture=self.trigger_update_size)
root.bind(texture=lambda *args: root.unbind(texture=self.trigger_update_size))
return root
def update_size(self, *args):
if self.root.texture and self.root.texture_size[0]:
if abs(self.root.texture_size[0] - self.root.width) > (self.root.texture_size[0] * 0.001):
xratio = float(self.root.width) / float(self.root.texture_size[0])
yratio = float(self.root.height) / float(self.root.texture_size[1])
ratio = min(xratio, yratio)
ofs = float(self.root.font_size)
fs = self.root.font_size * ratio
if abs(ofs - fs) > (ofs * 0.001):
self.root.font_size = max(1, fs)
print 'set font size to', self.root.font_size
if __name__ == '__main__':
TestApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment