I am building an app using kivy in which there are a bunch of buttons , labels and screens. My App is running fine inside PC and my personal android phone which i used for testing that app but when i use the app on other android phones having different display size ,the text does not resizes itself. So please help me what to use so that the text resizes according to display size.
--
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/6a11d660-1479-43fe-952a-4d7207a0ebbd%40googlegroups.com.
I'm also facing a similar issue.
On Wed, 6 May 2020 at 06:08, Parth Bissa <parth...@gmail.com> wrote:
I am building an app using kivy in which there are a bunch of buttons , labels and screens. My App is running fine inside PC and my personal android phone which i used for testing that app but when i use the app on other android phones having different display size ,the text does not resizes itself. So please help me what to use so that the text resizes according to display size.--
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-...@googlegroups.com.
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/b705cefb-aac6-4e4d-bfef-b85235f8fee8%40googlegroups.com.
The first thing to ensure you are doing is using the metrics module to specify the sizes in a way that should automatically adjust per platform.
For fonts this is sp(). You can express this syntax in 2 ways:
font_size: sp(20)
OR
font_size: ‘20sp’
read: https://kivy.org/doc/stable/api-kivy.metrics.html?highlight=sp#dimensions
If you are already using sp() or if this approach is not sufficient for your use case, there are a number of ways to scale the font size.
Let me know.
--
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/6a11d660-1479-43fe-952a-4d7207a0ebbd%40googlegroups.com.
The first thing to ensure you are doing is using the metrics module to specify the sizes in a way that should automatically adjust per platform.
For fonts this is sp(). You can express this syntax in 2 ways:
font_size: sp(20)
OR
font_size: ‘20sp’
read: https://kivy.org/doc/stable/api-kivy.metrics.html?highlight=sp#dimensions
If you are already using sp() or if this approach is not sufficient for your use case, there are a number of ways to scale the font size.
Let me know.
From: Parth Bissa
Sent: Wednesday, May 6, 2020 3:08 AM
To: Kivy users support
Subject: [kivy-users] Screen Size Issue in Kivy App
I am building an app using kivy in which there are a bunch of buttons , labels and screens. My App is running fine inside PC and my personal android phone which i used for testing that app but when i use the app on other android phones having different display size ,the text does not resizes itself. So please help me what to use so that the text resizes according to display size.
--
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-...@googlegroups.com.
I started to put a little experiment together to change the text size dynamically. I don’t have the time to finish it right now – but thought it might inspire you.
When the size of the button changes, the code compares the size of the text (texture_size) to the size of the widget and then adjusts. Current it is only checking for height. Ultimately it will test for height and width and get a best fit.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
kv = """
BoxLayout:
orientation: 'vertical'
ScaleButton:
text: 'Test One'
ScaleButton:
text: 'This is a test of a much longer string of text'
ScaleButton:
text: 'Yet another string'
"""
class ScaleButton(Button):
def on_size(self, *args):
print('size changed')
tw,th = self.texture_size
width,height = self.size
if height < width: # Grow or shrink height
if th < height: # Grow
while(th < height):
self.font_size += 1
self.texture_update()
th = self.texture_size[1]
else: # shrink
while (th > height):
self.font_size -= 1
self.texture_update()
th = self.texture_size[1]
class ScaleFontApp(App):
def build(self):
return Builder.load_string(kv)
ScaleFontApp().run()
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/242c00ff-69fa-4904-98c0-1c04a4fc3352%40googlegroups.com.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
kv = """
BoxLayout:
orientation: 'vertical'
ScaleButton:
text: 'Test One'
ScaleButton:
text: 'This is a test of a much longer string of text'
ScaleButton:
text: 'Yet another string'
"""
class ScaleButton(Button):
def on_size(self, *args):
t_width,t_height= self.texture_size
width,height = self.size
if t_height < height and t_width < width: # Grow
while t_height < height and t_width < width:
self.font_size += 1
self.texture_update()
t_width,t_height= self.texture_size
elif t_height > height or t_width > width: # shrink
while t_height > height or t_width > width:
self.font_size -= 1
self.texture_update()
t_width,t_height= self.texture_size
class ScaleFontApp(App):
def build(self):
return Builder.load_string(kv)
ScaleFontApp().run()
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/242c00ff-69fa-4904-98c0-1c04a4fc3352%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/242c00ff-69fa-4904-98c0-1c04a4fc3352%40googlegroups.com.
If you want the labels automatically sized to the position on the screen, use a layout.
Here is a small example using a vertical BoxLayout.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
kv = """
RootBoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'vertical'
id: label_space
BoxLayout:
size_hint_y: None
height: 48
Button:
text: 'Add Label'
on_release: root.add_size_label()
Button:
text: 'Reset'
on_release: root.remove_labels()
"""
class SizeLabel(Label):
def on_size(self, *args):
t_width,t_height= self.texture_size
width,height = self.size
if t_height < height and t_width < width: # Grow
while t_height < height and t_width < width:
self.font_size += 1
self.texture_update()
t_width,t_height= self.texture_size
elif t_height > height or t_width > width: # shrink
while t_height > height or t_width > width:
self.font_size -= 1
self.texture_update()
t_width,t_height= self.texture_size
class RootBoxLayout(BoxLayout):
def add_size_label(self):
w = SizeLabel(text='bla-bla-bla')
self.ids.label_space.add_widget(w)
def remove_labels(self):
child_widgets = self.ids.label_space.children.copy()
for child in child_widgets:
self.ids.label_space.remove_widget(child)
class SizeLabelApp(App):
def build(self):
return Builder.load_string(kv)
SizeLabelApp().run()
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/3517ee49-e34d-47ff-9382-60b8811f56fb%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/3517ee49-e34d-47ff-9382-60b8811f56fb%40googlegroups.com.
Post a runnable example. There is not enough here for me to help.
Is menu a screen? Is self a screen?
How do you want the Labels to layout?
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/ce1f93ea-a8bf-4b85-8d9e-fc8fe80fd272%40googlegroups.com.
<IndiaResult>
GridLayout:
rows : 10
ScaleButton :
text : "[b][color=#ff9900]GET[/color][color=#ffffff] RES[/color][color=#00cc00]ULTS[/color][/b] "
markup : True
background_color: (0.0, 0.0, 1.0,0.7)
on_press : root.india()
ScaleButton :
text : "[b][color=#ff9900]BACK[/color][color=#ffffff] TO[/color][color=#00cc00] MENU[/color][/b]"
markup : True
background_color: (0.0, 0.0, 1.0,0.7)
on_press : root.manager.current = 'menu'
Label :
text : ""
size_hint : 5,5
class IndiaResult(Screen):
def india(self):
try:
url = "https://covid-19-india-data-by-zt.p.rapidapi.com/GetIndiaTotalCounts"
headers = {
'x-rapidapi-host': "covid-19-india-data-by-zt.p.rapidapi.com",
'x-rapidapi-key': "3ae1125923msha231c2e1fc6c41ep14814cjsnfe458e46c62f"
}
time.sleep(1)
response = requests.request("GET", url, headers=headers)
x = response.text
parsed = json.loads(x)
g = parsed['data']
print(parsed)
CONFIRMED = g[0]['confirmed']
RECOVERED = g[0]['recovered']
DEATHS = g[0]['deaths']
print("CONFIRMED :" + CONFIRMED)
print("RECOVERED :" + RECOVERED)
print("DEATHS :" + DEATHS)
self.add_widget(ScLabel(text="[b][color=#FFFF00]TOTAL CONFIRMED : [/color][/b]" + str(CONFIRMED), markup=True))
self.add_widget(ScLabel(text="[b][color=#33FF7C]TOTAL RECOVERED : [/color][/b]"+ str(RECOVERED), markup=True))
self.add_widget(ScLabel(text="[b][color=#FF0000]TOTAL DEATHS : [/color][/b]"+ str(DEATHS), markup=True))
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/ce1f93ea-a8bf-4b85-8d9e-fc8fe80fd272%40googlegroups.com.
Currently you are adding the Labels to the screen, but not to the Layout. A Screen is a RelativeLayout, the Labels do not have position hints so they are overalaping.
Assuming you want to add the Labels to the GridLayout make the following changes:
<IndiaResult>
GridLayout:
rows : 10
Id: grid
(The rest of the kv code stays the same)
self.ids.grid.add_widget(ScLabel(text="[b][color=#FFFF00]TOTAL CONFIRMED : [/color][/b]" + str(CONFIRMED), markup=True))
self.ids.grid.add_widget(ScLabel(text="[b][color=#33FF7C]TOTAL RECOVERED : [/color][/b]"+ str(RECOVERED), markup=True))
self.ids.grid.add_widget(ScLabel(text="[b][color=#FF0000]TOTAL DEATHS : [/color][/b]"+ str(DEATHS), markup=True))
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/36bf5eab-a37a-4790-95a1-d773b5a13bc4%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/36bf5eab-a37a-4790-95a1-d773b5a13bc4%40googlegroups.com.
Id and ids are described here: https://kivy.org/doc/stable/api-kivy.lang.html?highlight=lang#ids
In short, when a kv file is parsed, the id in each widget goes into and ids dictionary. The key is the id, the value is the widget object. The root widget and each rule has an ids dict. Using the ids is a key part of navigating kivy. It provides a mechanism to access any widget from any class or location.
From: Parth Bissa
Sent: Tuesday, May 12, 2020 2:23 AM
To: Kivy users support
Subject: Re: [kivy-users] Screen Size Issue in Kivy App
Thank You So Much It Worked..................
What is this id btw never seen it in any kivy code?
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/36bf5eab-a37a-4790-95a1-d773b5a13bc4%40googlegroups.com.
--
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/4dbec044-3a34-4265-a3d3-e770ef25391c%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/4dbec044-3a34-4265-a3d3-e770ef25391c%40googlegroups.com.
You could try a few things to simplify the scaling code.
I put together an example that loads screens one at a time with a progress bar.
https://groups.google.com/d/msg/kivy-users/1ROv9VHTbaw/PiLoKQyhHAAJ
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/6ca527fe-bd82-47df-bb33-445ce8399ed6%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/6ca527fe-bd82-47df-bb33-445ce8399ed6%40googlegroups.com.
Share your code.
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/33c7cd85-6683-4d56-9164-921188e43a24%40googlegroups.com.
class ScaleButton(Button):
def on_size(self, *args):
t_width, t_height = self.texture_size
width, height = self.size
if t_height < height and t_width < width: # Grow
while t_height < height and t_width < width:
self.font_size += 1
self.texture_update()
t_width, t_height = self.texture_size
elif t_height > height and t_width > width: # shrink
while t_height > height and t_width > width:
self.font_size -= 20
self.texture_update()
t_width,t_height= self.texture_size
<IndiaResult>
GridLayout:
rows : 10
ScaleButton :
text : "[b][color=#ff9900]GET[/color][color=#ffffff] RES[/color][color=#00cc00]ULTS[/color][/b] "
markup : True
background_color: (0.0, 0.0, 1.0,0.7)
on_press : root.india()
ScaleButton :
text : "[b][color=#ff9900]BACK[/color][color=#ffffff] TO[/color][color=#00cc00] MENU[/color][/b]"
markup : True
background_color: (0.0, 0.0, 1.0,0.7)
on_press : root.manager.current = 'menu'
Label :
text : ""
size_hint : 5,5
class IndiaResult(Screen):
def india(self):
try:
url = "https://covid-19-india-data-by-zt.p.rapidapi.com/GetIndiaTotalCounts"
headers = {
'x-rapidapi-host': "covid-19-india-data-by-zt.p.rapidapi.com",
'x-rapidapi-key': "3ae1125923msha231c2e1fc6c41ep14814cjsnfe458e46c62f"
}
time.sleep(1)
response = requests.request("GET", url, headers=headers)
x = response.text
parsed = json.loads(x)
g = parsed['data']
print(parsed)
CONFIRMED = g[0]['confirmed']
RECOVERED = g[0]['recovered']
DEATHS = g[0]['deaths']
print("CONFIRMED :" + CONFIRMED)
print("RECOVERED :" + RECOVERED)
print("DEATHS :" + DEATHS)
self.ids.grid.add_widget(
ScLabel(text="[b][color=#FFFF00]TOTAL CONFIRMED : [/color][/b]" + str(CONFIRMED), markup=True))
self.ids.grid.add_widget(
ScLabel(text="[b][color=#33FF7C]TOTAL RECOVERED : [/color][/b]" + str(RECOVERED), markup=True))
self.ids.grid.add_widget(
ScLabel(text="[b][color=#FF0000]TOTAL DEATHS : [/color][/b]" + str(DEATHS), markup=True))
except requests.exceptions.ConnectionError :
def show_popup():
self.ids.grid.add_widget(ScLabel(text = "[b]KINDLY CHEK YOUR NETWORK CONNECTION[/b]",markup=True))
show_popup()
except json.JSONDecodeError:
def show_popup():
self.ids.grid.add_widget(ScLabel(text="[b]KINDLY CHEK YOUR NETWORK CONNECTION[/b]",markup=True))
show_popup()
pass
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/33c7cd85-6683-4d56-9164-921188e43a24%40googlegroups.com.
Please send a minimal runnable example.
There is something wrong here. In the python code you are using the id ‘grid’, but I don’t see in in the KV code.
Your exception handlers are declaring and calling the same code. The code is being declared in a nested way. This is not necessary. You are declaring the same function twice. Either elevate show_popup() to the same level as a method, or just use the have the single line of code you want to execute. Is this a placehoder?
Also, make an asynchronous call and use the completion callback or event. Do not use sleep. Your app will appear dead and unresponsive during sleep.
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/3d72f8a7-1d65-4a0f-84cf-661f318074a7%40googlegroups.com.
I made an enhancement where the minimum text size can be set. There was a possibility of getting into an endless loop If the font size went negative, that also has been fixed.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.properties import NumericProperty
kv = """
<ScaleButton>:
padding: 10,10
BoxLayout:
orientation: 'vertical'
Label:
text: 'Test ScaleButton in a GridLayout'
font_size: 20
size_hint_y: .1
GridLayout:
id: grid
rows: 10
"""
class ScaleButton(Button):
min_font_size = NumericProperty(5)
def on_size(self, *args):
t_width,t_height= self.texture_size
width,height = self.size
if t_height < height and t_width < width: # Grow
while t_height < height and t_width < width:
self.font_size += 1
self.texture_update()
t_width,t_height= self.texture_size
elif t_height > height or t_width > width: # shrink
while t_height > height or t_width > width:
self.font_size = max(self.font_size - 1, self.min_font_size)
if self.font_size == self.min_font_size:
break
self.texture_update()
t_width,t_height= self.texture_size
class ScaleFontApp(App):
def build(self):
return
Builder.load_string(kv)
def on_start(self):
for i in range(10):
sb = ScaleButton(text='This is Button '+str(i))
self.root.ids.grid.add_widget(sb)
ScaleFontApp().run()
Thank You For Explaining....
1. Add an id to the GridLayout in the kv code:
<IndiaResult>
GridLayout:
rows : 10Id: grid
(The rest of the kv code stays the same)
2. Add the Labels to the FridLayout, using the id. In the Python code:
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/3d72f8a7-1d65-4a0f-84cf-661f318074a7%40googlegroups.com.
Thank You For Explaining....
self.ids.grid.add_widget(ScLabel(text="[b][color=#33FF7C]TOTAL RECOVERED : [/color][/b]"+ str(RECOVERED), markup<span style='font-family:Co
Thank You For Explaining....
))
self<span style='font-family:Consolas;color:#A
Run my code. The font reduces in size and grows in size. Share your code, I will take a look at it.
If you want to reduce the number of iterations, increase the font scaling increment. In the example below it is 1, set it to 5 or 10.
In many ways this is overkill calculating a size for every instance. You could do a calculation like this on one widget, and use it to calculate a font_size that you use everywhere.
--
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/e889e39e-bf18-41a5-81e7-983d8b1dc5e1%40googlegroups.com.
You can use: https://kivy.org/doc/stable/api-kivy.network.urlrequest.html?highlight=url#module-kivy.network.urlrequest
And use callbacks for the error cases. The kivy function does the JSON decoding.
If you want to use requests and exception handlers:
except requests.exceptions.ConnectionError :
self.show_popup()
except json.JSONDecodeError:
self.show_popup()
def show_popup(self):
self.ids.grid.add_widget(ScLabel(text="[b]KINDLY CHEK YOUR NETWORK CONNECTION[/b]",markup=True))
You might consider putting your try except where India is called, putting it as high up in the call stack as possible, will enable you to create better exception handling.
--
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/7f31e510-e7e1-4eaa-8508-febacd64129b%40googlegroups.com.
Thank You For Explaining....
KV FILE : :
 
Thank You For Explaining....
Here are a few things to thing about.
You have almost the same code repeated many times. This type of code is difficult to maintain and very error prone. If you have a mistake, you need to fix it over, and over…
Without looking too closely it appears you have the same code, what changes is the index into a list. Consider creating one method to read the data, and pass in the index. Better yet create a dict that associates the name of the screen with the appropriate index.
Lots of your screens also all have the same structure. You can create one screen class and instance it multiple times. Customize it with the data that needs to be unique to each screen.
This will dramatically reduce the amount of code, and make it much more maintainable. Your screenmanager might end up looking like:
ScreenManager:
id: scr_mngr
size_hint_y : None
height : root.height - toolbar.height
Welcome :
id:sc0
DataScreen:
name: ‘Assam’
DataScreen:
name: ‘Haryana’
--
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/6b147417-0d1b-4c09-9def-40c08db7af1f%40googlegroups.com.
Try to get a common DataScreen working for 2 locations. After it is working, you can easily scale it out to all the locations you want to represent.
--
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/3fddceab-fe51-462a-a7a5-6ee3319a2e36%40googlegroups.com.
The code attached is an example. I stripped out a few things from your code.
All of the results come back with one URL request, so I only do one request, rather than a request per screen.
You were creating widgets in the gridlayout on each screen, each time you visited the screen, but never clearing them. I allocated the labels in kv, and filled them from python.
happy to answer any questions.
--
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/0f0aa6e5-7586-4366-835a-c05d57d03ff3%40googlegroups.com.
Share your code.
<p class=MsoNormal style='mso-margin-top-alt:auto;ms
I suspect you can put the rest of your widgets/design elements as you had them before. I did not spend anytime looking at that part of the code. I haven’t used KivyMD, so I kept it all in kivy for the example.
The EventDispatcher is not required for this code. You can remove it, changing the class declaration to:
class CV19Data:
If you are going to use kivy properties, and no widgets, you would need to have a class derived from EventDispatcher. I will often put the code returned from this kind of API call into a kivy property so screens update automatically, we can do that here as well, but I didn’t and its more in keeping with how you wrote the screen. We could create a list of the attributes we want to display in a list property, and put those kivy properties on the screen. No real benefit over writing the value to the labels as you have done.
You could reduce the code even further by dynamically creating the screens in the ScreenManager, by looping over the locations dict.
--
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/2cbf90b3-a8b1-4001-a83b-e83fc0840735%40googlegroups.com.