How to show loading screen at start of functionin Kivy - kivy

I am a beginner on Kivy and need some help.
I have a basic mathematical application that, when i press a button it also takes 2-3 secs to be completed the function and then app shows us the result. During this process i want my app to show a loading screen
I found this solution but could not compile for my code
https://stackoverflow.com/a/66154747/19925544
Could you please help me to fix this
Thanks.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.metrics import dp
import requests
class WrappedLabel(Label):
pass
class Math(BoxLayout):
def __init__(self, **kwargs):
super(Predictor, self).__init__(**kwargs)
self.status = True
self.data = self.datas()
self.Today = self.today()
def underOver(self, s_image, screenmanager):
if(screenmanager.current == 'homepage_screen' or screenmanager.current == 'side_screen' or screenmanager.current == 'privacy_policy_screen'):
screenmanager.transition.direction = 'left'
screenmanager.current = 'result_screen'
print("""
Below the calculate codes which are inside underOver function
""")
KV File
Math:
<Math>:
ScreenManager:
id: sm
size: root.width, root.height
Screen:
name: 'result_screen'
Image:
source: 'images/result_background.png'
allow_stretch: True
keep_ratio: False
BoxLayout:
spacing: 20
orientation: 'vertical'
BoxLayout:
size_hint: 1, 0.10
Label:
text: '#'
font_name: 'fonts/Lcd.ttf'
font_size: '30dp'
color: 1, 0.4, 0.769, 1
Label:
text: 'T1'
font_name: 'fonts/Lcd.ttf'
font_size: '30dp'
color: 1, 0.4, 0.769, 1
Label:
text: 'T2'
font_name: 'fonts/Lcd.ttf'
font_size: '30dp'
color: 1, 0.4, 0.769, 1
Label:
text: 'G.A'
font_name: 'fonts/Lcd.ttf'
font_size: '30dp'
color: 1, 0.4, 0.769, 1
Label:
text: '2.5'
font_name: 'fonts/Lcd.ttf'
font_size: '30dp'
color: 1, 0.4, 0.769, 1
Label:
text: '3.5'
font_name: 'fonts/Lcd.ttf'
font_size: '30dp'
color: 1, 0.4, 0.769, 1
BoxLayout:
size_hint: 1, 0.80
ScrollView:
bar_margin: 5
bar_color: 1, 0.4, 0.769, 1
bar_width: 10
bar_inactive_color: 1, 0.4, 0.769, 1
GridLayout:
id: gridsonuc
cols: 1
spacing: 15
size_hint_y: None
height: self.minimum_height
BoxLayout:
size_hint: 1, 0.10
Button:
id: home_button_result
on_press: root.homepage(img_home, sm)
background_color: 0, 0, 0, 0
Image:
id: img_home
source: 'images/home_button.png'
allow_stretch: True
keep_ratio: False
size: self.parent.size
pos: home_button_result.pos
Button:
id: side_button_result
on_press: root.side(img_side, sm)
background_color: 0, 0, 0, 0
Image:
id: img_side
source: 'images/side_button.png'
allow_stretch: True
keep_ratio: False
size: self.parent.size
pos: side_button_result.pos

Related

How do I remove extra unwanted space in kivy BoxLayout?

<BoxExample#BoxLayout>:
orientation: "vertical"
BoxLayout:
orientation: "horizontal"
size_hint_y: None
Button:
text: "Logo"
size_hint: None, None
width: "80dp"
height: "40dp"
pos_hint: {'top': 1}
Button:
text: "Menu"
size_hint: 1, None
height: "40dp"
pos_hint: {'top': 1}
Button:
text: "Body"
Button:
text: "Footer"
size_hint: 1, None
height: "40dp"
That black space in the picture above, which I want to get rid of.
However after adding size_hint_y: None the gap has been narrowed as in the picture.
It is due to BoxLayout which provides each of its child widgets equal space but here you have changed the size of buttons which are inside of BoxLayout.You also need to change the size of BoxLayout Otherwise it will show black.
<BoxExample#BoxLayout>:
orientation: "vertical"
BoxLayout:
orientation: "horizontal"
size_hint: None,None
size:root.size[0],"40dp"
Button:
text: "Logo"
size_hint: None, None
width: "80dp"
height: "40dp"
pos_hint: {'top': 1}
Button:
text: "Menu"
size_hint: 1, None
height: "40dp"
pos_hint: {'top': 1}
Button:
text: "Body"
Button:
text: "Footer"
size_hint: 1, None
height: "40dp"
Just add:
height: self.minimum_height
to the BoxLayout:
<BoxExample#BoxLayout>:
orientation: "vertical"
BoxLayout:
orientation: "horizontal"
size_hint_y: None
height: self.minimum_height
Button:
text: "Logo"
size_hint: None, None
width: "80dp"
height: "40dp"
pos_hint: {'top': 1}
Button:
text: "Menu"
size_hint: 1, None
height: "40dp"
pos_hint: {'top': 1}
Button:
text: "Body"
Button:
text: "Footer"
size_hint: 1, None
height: "40dp"

kivy KeyError in an ids dictionary

I have the following code snippet:
def on_articles(self, *args):
#self.parent.ids['recommendations'].update_recommendations(self.articles)
self.parent.children[0].update_recommendations(self.articles)
The commented line does not work, because self.parent.ids is an empty dictionary ({})
The uncommented line does work. Why? How to best refer to Recommendations widget?
Here is the UI.
Builder.load_string('''
<SearchItem>:
canvas.before:
Color:
rgba: [0.8, 0.8, 0.8, 1] if self.state == 'normal' else [30/255, 139/255, 195/255, 1]
Rectangle:
size: self.size
pos: self.pos
Color:
rgba: 0, 0, 0, 1
Line:
rectangle: self.x, self.y, self.width, self.height
color: 0, 0, 0, 1
<Urlpopup>:
size_hint: .7, .7
auto_dismiss: False
title: ''
BoxLayout:
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
size: self.size
pos: self.pos
orientation: 'vertical'
padding: self.width * 0.1
spacing: self.height * 0.1
Spinner:
id: spinner
size_hint: None, None
size: 100, 44
text: 'en'
values: 'en', 'fr', 'de', 'it'
SearchBar:
id: searchbar
size_hint: 1, 0.4
multiline: False
font_size: self.height*0.8
Recommendations:
id: recommendations
orientation: 'vertical'
SearchItem
''')

How to use Hebrew font in Kivy?

I try to use Hebrew font in kivy app and after explore i use LabelBase.register and change my reading of the kv file with encoding='utf-8' but still get error with the line: font_name.
my main.py:
# main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.core.text import LabelBase
LabelBase.register(name="Arial", fn_regular="Arial.ttf")
class exist_form_window(Screen):
project_name = ObjectProperty(None)
manage_name = ObjectProperty(None)
def create(self):
if (self.project_name.text):
self.reset()
sm.current = "main"
else:
invalidLogin()
def reset(self):
self.project_name.text =""
self.manage_name.text = ""
class new_form_window(Screen):
project_name = ObjectProperty(None)
manage_name = ObjectProperty(None)
def create(self):
if (self.project_name.text):
self.reset()
sm.current = "main"
else:
invalidLogin()
def reset(self):
self.project_name.text =""
self.manage_name.text = ""
class MainWindow(Screen):
def new_form(self):
sm.current = "new_form"
def exist_form(self):
sm.current = "exist_form"
class WindowManager(ScreenManager):
pass
def invalidLogin():
pop = Popup(title='שגיאה',
content=Label(text='חלק מהערכים חסרים'),
size_hint=(None, None), size=(400, 400))
pop.open()
with open("roniapp.kv", encoding='utf-8') as f:
Builder.load_string(f.read())
sm = WindowManager()
screens = [MainWindow(name="main"),new_form_window(name="new_form"),exist_form_window(name="exist_form")]
for screen in screens:
sm.add_widget(screen)
sm.current = "main"
class MyMainApp(App):
def build(self):
return sm
if __name__ == "__main__":
MyMainApp().run()
My roniapp.kv file:
<MainWindow>:
name: "main"
FloatLayout:
Label:
font_name: 'Arial'
text:"Asad"
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint: {"x":0.1, "top":0.9}
size_hint: 0.35, 0.15
Button:
pos_hint:{"x":0.2, "y": 0.3}
size_hint:0.6,0.2
text: "aaa"
on_release:
root.new_form()
root.manager.transition.direction = "down"
Button:
pos_hint:{"x":0.2, "y": 0.1}
size_hint:0.6,0.2
text: "aaa"
on_release:
root.exist_form()
root.manager.transition.direction = "left"
<new_form_window>:
name: "new_form"
project_name: project_name
manage_name: manage_name
FloatLayout:
Label:
text:"aaa"
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint: {"x":0.1, "top":0.9}
size_hint: 0.35, 0.15
TextInput:
id: project_name
font_size: (root.width**2 + root.height**2) / 13**4
multiline: False
pos_hint: {"x": 0.45 , "top":0.9}
size_hint: 0.4, 0.15
Label:
text:"aaa"
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint: {"x":0.1, "top":0.7}
size_hint: 0.35, 0.15
TextInput:
id: manage_name
font_size: (root.width**2 + root.height**2) / 13**4
multiline: False
pos_hint: {"x": 0.45, "top":0.7}
size_hint: 0.4, 0.15
Button:
pos_hint:{"x":0.2,"y":0.05}
size_hint: 0.6, 0.2
font_size: (root.width**2 + root.height**2) / 13**4
text: "aaa"
on_release:
root.manager.transition.direction = "left"
root.create()
<exist_form_window>:
name: "exist_form"
project_name: project_name
manage_name: manage_name
FloatLayout:
Label:
text:"aaa"
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint: {"x":0.1, "top":0.9}
size_hint: 0.35, 0.15
TextInput:
id: project_name
font_size: (root.width**2 + root.height**2) / 13**4
multiline: False
pos_hint: {"x": 0.45 , "top":0.9}
size_hint: 0.4, 0.15
Label:
text:"aaa"
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint: {"x":0.1, "top":0.7}
size_hint: 0.35, 0.15
TextInput:
id: manage_name
font_size: (root.width**2 + root.height**2) / 13**4
multiline: False
pos_hint: {"x": 0.45, "top":0.7}
size_hint: 0.4, 0.15
Button:
pos_hint:{"x":0.2,"y":0.05}
size_hint: 0.6, 0.2
font_size: (root.width**2 + root.height**2) / 13**4
text: "aaa"
on_release:
root.manager.transition.direction = "left"
root.create()
And get this error:
File "C:\Users\adi\Anaconda3\envs\project_2\lib\site-packages\kivy\lang\parser.py", line 584, in
parse_level
'Invalid data after declaration')
ParserException: Parser: File "<inline>", line 6:
...
4: FloatLayout:
5: Label:
>> 6: font_name: 'Arial'
7: text:"Asad"
8: font_size: (root.width**2 + root.height**2) / 13**4
...
Invalid data after declaration
If it's because i save the kv file with notepad i try with different apps and still get this error.
thanks for help!
The kivy Parser is throwing an error because the code in your .kv file is not formed correctly. Your indentation needs to be fixed. I managed to run your app on my machine by keeping your python code unchanged and replacing your .kv file with the following:
<MainWindow>:
name: "main"
FloatLayout:
Label:
font_name: 'Arial'
text:"Asad"
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint: {"x":0.1, "top":0.9}
size_hint: 0.35, 0.15
Button:
pos_hint:{"x":0.2, "y": 0.3}
size_hint:0.6,0.2
text: "aaa"
on_release:
root.new_form()
root.manager.transition.direction = "down"
Button:
pos_hint:{"x":0.2, "y": 0.1}
size_hint:0.6,0.2
text: "aaa"
on_release:
root.exist_form()
root.manager.transition.direction = "left"
<new_form_window>:
name: "new_form"
project_name: project_name
manage_name: manage_name
FloatLayout:
Label:
text:"aaa"
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint: {"x":0.1, "top":0.9}
size_hint: 0.35, 0.15
TextInput:
id: project_name
font_size: (root.width**2 + root.height**2) / 13**4
multiline: False
pos_hint: {"x": 0.45 , "top":0.9}
size_hint: 0.4, 0.15
Label:
text:"aaa"
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint: {"x":0.1, "top":0.7}
size_hint: 0.35, 0.15
TextInput:
id: manage_name
font_size: (root.width**2 + root.height**2) / 13**4
multiline: False
pos_hint: {"x": 0.45, "top":0.7}
size_hint: 0.4, 0.15
Button:
pos_hint:{"x":0.2,"y":0.05}
size_hint: 0.6, 0.2
font_size: (root.width**2 + root.height**2) / 13**4
text: "aaa"
on_release:
root.manager.transition.direction = "left"
root.create()
<exist_form_window>:
name: "exist_form"
project_name: project_name
manage_name: manage_name
FloatLayout:
Label:
text:"aaa"
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint: {"x":0.1, "top":0.9}
size_hint: 0.35, 0.15
TextInput:
id: project_name
font_size: (root.width**2 + root.height**2) / 13**4
multiline: False
pos_hint: {"x": 0.45 , "top":0.9}
size_hint: 0.4, 0.15
Label:
text:"aaa"
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint: {"x":0.1, "top":0.7}
size_hint: 0.35, 0.15
TextInput:
id: manage_name
font_size: (root.width**2 + root.height**2) / 13**4
multiline: False
pos_hint: {"x": 0.45, "top":0.7}
size_hint: 0.4, 0.15
Button:
pos_hint:{"x":0.2,"y":0.05}
size_hint: 0.6, 0.2
font_size: (root.width**2 + root.height**2) / 13**4
text: "aaa"
on_release:
root.manager.transition.direction = "left"
root.create()
And it works:

Kivy ScrollView with two Labels and simultaneously scrolling?

I have created a UI that displays two labels with long text in the lower area. Both labels are in a separate scrollview and therefore scrollable. How can I combine both labels into a single scrollview so that both labels are scrollable at the same time? Do I need to create another Layout around a Scrollview?
Here is my kv:
<Button>:
font_size: 20
color: 0.1,0.5,0.6,1
size_hint: 0.3,0.1
<Label>:
size_hint: 0.1,0.1
<TextInput>:
size_hint: 1,0.1
<PopupBox>:
size_hint: .2, .15
auto_dismiss: True
title: 'Please wait'
Label:
pos_hint: {"x":0.5, "top":0.7}
text: 'Creating the data...'
<FloatLayout>:
canvas:
Color:
rgba: 0.15, 0.15, 0.15, 0.15
Rectangle:
size: self.size
pos: self.pos
Label:
text: "Label 1"
size_hint: 0.1,0.1
pos_hint: {"x":0.0, "top":1}
TextInput:
id: input1
pos_hint: {"x":0.11, "top":1}
multiline: True
Label:
text: "Label 2"
size_hint: 0.1,0.1
pos_hint: {"x":0.0, "top":0.9}
TextInput:
id: input2
pos_hint: {"x":0.11, "top":0.9}
multiline: True
Label:
text: "Label 3"
size_hint: 0.11, None
pos_hint: {"x":0, "top":0.8}
height: 30
TextInput:
id: input3
text: "|"
pos_hint: {"x":0.11, "top":0.8}
size_hint: 0.03, None
height: 30
Button:
text: "Clear"
on_press: app.clear_inputs()
pos_hint: {"x":0.15, "top":0.8}
size_hint: 0.1, None
height: 30
Label:
id: dd_label
text: "dd"
pos_hint: {"x":0.7, "top":0.8}
size_hint: 0.1, None
height: 30
Button:
text: "Comp"
on_press: app.start_second_thread()
pos_hint: {"x":0, "top":0.76}
size_hint: 1, None
height: 50
Button:
text: "So"
on_press: app.show_popup()
pos_hint: {"x":0, "top":0.69}
size_hint: 0.1, None
height: 25
GridLayout:
cols: 2
size_hint: 1, 0.67
ScrollView:
size_hint: (1, 1)
bar_width: 10
bar_color: 0.1, 0.1, 0.1, 0.1
bar_inactive_color: 0.15, 0.15, 0.15, 0.15
effect_cls: "ScrollEffect"
scroll_type: ['bars']
Label:
id: f_output1
text: "long text1 \n" * 100
size_hint_y: None
size_hint_x: 0.7
size: self.texture_size
pos_hint: {"x":0.6, "top":0.7}
markup: True
text_size: 700, None
valign: "middle"
halign: 'right'
padding_x: 5
ScrollView:
size_hint: (1, 1)
bar_width: 10
bar_color: 0.1, 0.1, 0.1, 0.1
bar_inactive_color: 0.15, 0.15, 0.15, 0.15
effect_cls: "ScrollEffect"
scroll_type: ['bars']
Label:
id: f_output2
text: "long text2 \n" * 100
size_hint_y: None
size_hint_x: 1
size: self.texture_size
pos_hint: {"x":0.6, "top":0.7}
text_size: 600, None
markup: True
You can make a single ScrollView and make create a GridLayout with cols:2 for 2 labels
ScrollView:
size_hint: (1, 1)
bar_width: 10
bar_color: 0.1, 0.1, 0.1, 0.1
bar_inactive_color: 0.15, 0.15, 0.15, 0.15
effect_cls: "ScrollEffect"
scroll_type: ['bars']
GridLayout:
cols: 2
size_hint_y:None
height:self.minimum_height
Label:
id: f_output1
text: "long text1 \n" * 100
size_hint_y: None
size_hint_x: 0.7
size: self.texture_size
pos_hint: {"x":0.6, "top":0.7}
markup: True
text_size: 700, None
valign: "middle"
halign: 'right'
padding_x: 5
Label:
id: f_output2
text: "long text2 \n" * 100
size_hint_y: None
size_hint_x: 1
size: self.texture_size
pos_hint: {"x":0.6, "top":0.7}
text_size: 600, None
markup: True

Can not get widgets positioned in combi of GridLayout and BoxLayout using Kivy

I am trying to implement a fixed size UI consisting of 4 columns according to the structure below (see link "Wired frame"). At column 3 I got lost after hours of studying, looking at examples and a lot of trial and error. Who can help me? Below the kv content as well.
Thanks in advance.
Wired frame
<IeditHome>:
GridLayout:
cols: 4
# 1st Column
BoxLayout:
orientation: 'vertical'
Label:
canvas.before:
Color:
rgb: .7,.7,.7
Rectangle:
pos: self.pos
size: self.size
BorderImage:
source: 'C:/images/border.png'
border: (2,2,2,2)
size: self.size
pos: self.pos
text: '1'
size_hint: None, None
height: 120
width: 25
halign: 'right'
valign: 'middle'
color: .4,.4,.4,1
# Add 4 Label bloks of the same.
# Re-engineer duplicated stuff later...
# 2nd Column
BoxLayout:
orientation: 'vertical'
Image:
canvas.before:
Color:
rgb: .7,.7,.7
Rectangle:
pos: self.pos
size: self.size
BorderImage:
source: 'C:/images/border.png'
border: (2,1,1,2)
size: self.size
pos: self.pos
size_hint_x: None
size_hint_y: None
size: 240, 120
source: 'C:/temp/Object1.png'
# Add 4 image bloks of the same.
# Re-engineer duplicated stuff later...
# 3rd Column
BoxLayout:
orientation: 'vertical'
Label:
canvas.before:
Color:
rgb: .7,.7,.7
Rectangle:
pos: self.pos
size: self.size
text: 'This is the titel'
Image:
canvas.before:
Color:
rgb: .7,.7,.7
Rectangle:
pos: self.pos
size: self.size
BorderImage:
source: 'C:/images/border.png'
border: (2,1,1,2)
size: self.size
pos: self.pos
size_hint_x: None
size_hint_y: None
size: 300, 200
source: 'C:/temp/MediumImage.png'
Image:
canvas.before:
Color:
rgb: .7,.7,.7
Rectangle:
pos: self.pos
size: self.size
BorderImage:
source: 'C:/images/border.png'
border: (2,1,1,2)
size: self.size
pos: self.pos
size_hint_x: None
size_hint_y: None
size: 500, 300
source: 'C:/temp/LargeImage.png'
ActionBar:
pos: 0, 600
font_size: 16
size: 1200, 40
# background color in Kivy acts as a tint and not just a solid color.
# set a pure white background image first.
background_image: 'C:/images/white-bg.png'
background_color: .2,.6,.7,1
ActionView:
ActionPrevious:
size: 1200, 40
font_size: 16
title: 'Ieditor'
with_previous: False
ActionOverflow:
ActionButton:
icon: 'C:/images/butt_prev.png'
ActionButton:
icon: 'C:/images/butt_next.png'
Here is a small working example of the 3rd column should look like (The A class)
Builder.load_string("""
<InputBar#BoxLayout>:
orientation: 'horizontal'
Label:
text: 'some text'
TextInput:
text: 'input?'
Label:
text: 'some text2'
TextInput:
text: 'input2?'
<A>:
orientation: 'vertical'
Label:
text: 'some title'
InputBar:
id: input1
InputBar:
id: input2
InputBar:
id: input3
InputBar:
id: input4
Label:
text: 'some title2'
InputBar:
id: input5
InputBar:
id: input6
InputBar:
id: input7
InputBar:
id: input8
InputBar:
id: input9
""")
class A(BoxLayout):
pass
class VApp(App):
def build(self):
return A()
if __name__ == '__main__':
VApp().run()
Just place it inside your big example I guess.

Resources