How to set Font size or Label size to fit all device

2024/9/20 19:19:40

How to set kivy font size or label size so that it will fit all phone-device screen? (fit by means does not overlap with the boundary, rectangle, or even the screen)

I know the Buttons and some other Widgets in Kivy have size_hint. This size_hint does not give desired result.

Also, setting the font_size can do the trick, but it will be difficult to automate using this. Now close solution is by setting and getting the Window.size in cm, and use font_size in cm.

Thanks in advance.

Answer

This is actually interesting question.

Usually you pass text, font, font size and region that limits the text (text_size in kivy label) to renderer and get texture of some size from it. What you want is knowing label's texture size to get font size. I guess it's theoretically possible, but you'll need to do all calculations manually in Kivy.

May us do some workaround? Instead of trying to calculate font size, we can just use some very large one and scale final texture to match given size. Scaling with ration keeping can be easily done with Kivy's Image widget, for example. Proof-of-Concept (Python 3):

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.label import Labelclass MyLabel(Image):text = StringProperty('')def on_text(self, *_):# Just get large texture:l = Label(text=self.text)l.font_size = '1000dp'  # something that'll give texture bigger than phone's screen sizel.texture_update()# Set it to image, it'll be scaled to image size automatically:self.texture = l.textureclass RootWidget(BoxLayout):passclass TestApp(App):def build(self):return MyLabel(text='Test test test')if __name__ == '__main__':TestApp().run()

Far from ideal, but does job.

Result:

enter image description here

enter image description here

https://en.xdnf.cn/q/72274.html

Related Q&A

Using tweepy to access Twitters Streaming API

Im currently having trouble getting example code for using tweepy to access Twitters Streaming API to run correctly (err...or at least how I expect it to run). Im using a recent clone of tweepy from Gi…

Jupyter notebook, how to run multiple cells simultaneously?

I defined a python function which run a bash script. Lets say the function is: calc(x,y,z). If I run this function in python with some variables,>>> calc(1,2,3)It generates a C code which simu…

How to make a slice of DataFrame and fillna in specific slice using Python Pandas?

The problem: let us take Titanic dataset from Kaggle. I have dataframe with columns "Pclass", "Sex" and "Age". I need to fill NaN in column "Age" with a median f…

Pythons difflib SequenceMatcher speed up

Im using difflib SequenceMatcher (ratio() method) to define similarity between text files. While difflib is relatively fast to compare a small set of text files e.g. 10 files of 70 kb on average compar…

create an asymmetric colormap

I am creating a colormap to map colors in a folium choropleth map, using code from here:from branca.colormap import linearcolormap = linear.RdBu.scale(df.MyValue.min(),df.MyValue.max())colormapAs you c…

NLTK - Get and Simplify List of Tags

Im using the Brown Corpus. I want some way to print out all the possible tags and their names (not just tag abbreviations). There are also quite a few tags, is there a way to simplify the tags? By sim…

PolynomialFeatures object has no attribute predict

I want to apply k-fold cross validation on the following regression models:Linear Regression Polynomial Regression Support Vector Regression Decision Tree Regression Random Forest RegressionI am able t…

Error module object has no attribute freetype

I am using this code Link but it displays error of module object has no attribute i tried to pip install freetype but nothing happened. Can anyone please guide me with this.import cv2 import numpy as …

Count total number of white pixels in an image

I am trying to count total number of white pixels in the following image:But with my code, I get this errorsrc is not a numpy array, neither a scalar.This is my code: img=cv2.imread(filename,1) TP= wid…

Pass a JSON object to an url with requests

So, I want to use Kenneth excellent requests module. Stumbled up this problem while trying to use the Freebase API.Basically, their API looks like that:https://www.googleapis.com/freebase/v1/mqlread?q…