How to dynamically resize label in kivy without size attribute

2024/10/12 16:30:19

So, I get that you can usually just use self(=)(:)texture_size (py,kv) but all of my widgets are either based on screen(root only) or size_hint. I am doing this on purpose for a 'cross-platform' GUI. I open it on my android and the text is either too small or running off the screen i dont want to wrap it only resize.

What properties of the Label can I set so that it auto adjusts the font to fill the parent height and width (which is not explicitly defined)?

The text on the following Labels is printing at the default font size of 14 instead.

Example:

https://pastebin.com/95qA44QD

code
Answer

You really should follow the suggestion from @eyllanesc. But here is one way to do what you want (if I am interpreting your question correctly):

from functools import partialfrom kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.app import runTouchApp
from kivy.uix.textinput import TextInputclass RootWidget(GridLayout):def __init__(self, **kwargs):# prevent overridesuper(RootWidget, self).__init__(**kwargs)self.cols = 1self.email_label =  Label(color=(1, .5, .5, 1),text="Email:",size_hint=(1, 1))self.add_widget(self.email_label)self.email = TextInput(text='',foreground_color=(1, .5, .5, 1),multiline=False,size_hint=(1, 1))self.add_widget(self.email)self.add_widget(Label(color=(1, .5, .5, 1),text="Password:",size_hint=(1, 1)))self.pw = TextInput(text='',foreground_color=(1, .5, .5, 1),multiline=False,password=True,size_hint=(1, 1))self.add_widget(self.pw)self.login = Button(color=(1, .5, .5, 1),background_color=(0, 0, 0, 1),text="Login",size_hint=(1, 4))self.add_widget(self.login)self.login.bind(on_press=partial(self.checkuser,self.email,self.pw))self.bind(size=self.do_resize)def checkuser(self, *args):passdef do_resize(self, rootWidgt, new_size):self.email_label.font_size = 0.025 * new_size[1]if __name__ == '__main__':runTouchApp(RootWidget())

Simply put, save references to the things you want to adjust dynamically, add a binding to call do_resize() whenever your RootWidget is resized, and put code in there to make the adjustments you want. Note that the do_resize method will be called on the first display of RootWidget.

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

Related Q&A

How to parallelize this nested loop in Python that calls Abaqus

I have the nested loops below. How can i parallelize the outside loop so i can distribute the outside loop into 4 simultaneous runs and wait for all 4 runs to complete before moving on with the rest of…

Comparing one column value to all columns in linux enviroment

So I have two files , one VCF that looks like88 Chr1 25 C - 3 2 1 1 88 Chr1 88 A T 7 2 1 1 88 Chr1 92 A C 16 4 1 1and another with genes that looks likeGENEI…

Can be saved into a variable one condition?

It would be possible to store the condition itself in the variable, rather than the immediate return it, when to declare it?Example:a = 3 b = 5x = (a == b) print(x)a = 5 print(x)The return isFalse Fal…

Pycharm, can not find Python version 2.7.11

I installed Python 2.7.11 on this Mac, and from terminal Python 2.7.11 can be started. However, From the interpreter of Pycharm (2016.1 version) , there is no Python 2.7.11.Any suggestions ? ThanksPS…

Python Convert Binary String to IP Address in Dotted Notation

So Im trying to read in a file with some binary strings, i.e: 10000010 00000000 0000**** ********. The script will convert the *s to both 0 and 1, so there will be two binary strings that look like thi…

Scrapy: Extracting data from source and its links

Edited question to link to original:Scrapy getting data from links within tableFrom the link https://www.tdcj.state.tx.us/death_row/dr_info/trottiewillielast.htmlI am trying to get info from the main t…

Rename file on upload to admin using Django

I have used a function in Django 1.6 to rename my files when they are uploaded through admin, but this does not work in Django 1.8. Anyone know if it is still possible to do this in 1.8?class Entry(mo…

Ignore newline character in binary file with Python?

I open my file like so :f = open("filename.ext", "rb") # ensure binary reading with bMy first line of data looks like this (when using f.readline()):\x04\x00\x00\x00\x12\x00\x00\x00…

RegEx Parse Error by Parsley Python

I have made a simple parser for simple queries, to fetch data from a datastore. The operands I have used are <,<=,>,>=,==,!= The Parser works fine for every operand except for < I am a b…

Accessing Bangla (UTF-8) string by index in Python

I have a string in Bangla and Im trying to access characters by index.# -*- coding: utf-8 -*- bstr = "তরদজ" print bstr # This line is working fine for i in bstr:print i, # question marks …