Kivy how to update a Layout/Graphics in python

350 views
Skip to first unread message

Orenge

unread,
May 18, 2022, 2:40:28 PM5/18/22
to Kivy users support

I'm struggling to correctly update the Layouts (graphically) in Kivy after I modified them in python.

For example: I'm trying to build a GridLayout which changes its content (child_widgets, for now mostly buttons) depending on a button clicked on another screen: Here is the code of the Button that initializes the change in window and the modification of the GridLayout (GridLayout is nested in a Scrollview which is nested in a RelativLayout):

class Categorybutton(Button):
def __init__(self,gamename,type, **kwargs):
super().__init__(**kwargs)
 self.gamename = gamename
 self.type = type
 self.size_hint = (0.2,None)
 self.height = "80dp"
 self.Allcategories = {"category1": ("cat1","cat2","cat3"), "categroy2": ("cat4","cat5")}
def on_press(self, name="categorywindow"): App.get_running_app().root.get_screen("categorywindow").children[0].children[0].children[0].reconstruct(minigame=self.gamename, categories=self.Allcategories[self.type])
 App.get_running_app().root.current = name


And here the GridLayoutclass:

class dynamicalcategory(GridLayout): def __init__(self, **kwargs):          super().__init__(**kwargs) self.bind(minimum_height=self.setter('height'))
 self.cols = 1
 self.pos_hint = {"center_x" : 0.5, "center_y": 0.5}
 self.size_hint = (1, None)
 self.size_hint_y = None
def reconstruct(self, minigame, categories):
     self.clear_widgets()
for cat in categories:
     self.add_widget(Catsbutton(text=cat, gamename=minigame))


And the kv file I added as picture.

Groups.png

The result is however multiple buttons stacked upon each other and the GridLayout is out of place. This might be due to the fact that the GridLayout is initialised before the kv file is read. Also the GridLayout seems to ignore everything done in the kivy file. How do i fix this, i.e how do i tell my Gridlayout (defined in the script) to follow the rules defined in the kv file or how do I update it in the script?

I'm a beginner at using GUI and help would be much Appreciated.

Elliot Garbus

unread,
May 18, 2022, 3:05:04 PM5/18/22
to kivy-...@googlegroups.com

Please attach a file that I can run or a minimal example that demonstrates the problem.

To paste code into the website, if you right click and select paste as plain text the formatting will be preserved.

The result is however multiple buttons stacked upon each other and the GridLayout is out of place. This might be due to the fact that the GridLayout is initialised before the kv file is read. Also the GridLayout seems to ignore everything done in the kivy file. How do i fix this, i.e how do i tell my Gridlayout (defined in the script) to follow the rules defined in the kv file or how do I update it in the script?

I'm a beginner at using GUI and help would be much Appreciated.

--
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/3fcd0942-0d79-4c6c-a6ab-a5e9180dfd3cn%40googlegroups.com.

 

Groups.png
Message has been deleted

Orenge

unread,
May 18, 2022, 4:27:35 PM5/18/22
to Kivy users support
I keep messing up the formatting so i added the main and kv file.
Basically my aim is to put the dynamicalLayout inside the Scrollview defined in the kv file.(and adapt its  size) (Yellow canvas).
example.kv
main.py

Elliot Garbus

unread,
May 19, 2022, 7:22:23 PM5/19/22
to kivy-...@googlegroups.com

I’m not exactly sure I understand what you are trying to achieve.  I’ve modified your code and added a few features that I hope will help you better understand kivy.

Here are a few tips to keep in mind.

 

Other comments on the code:

  • You had a lot of code in python that was statically creating widgets.  I moved these to kv, I find it easier to read.  I also moved the creation of the root widget to kv.
  • I think the problem you had with your layout was the use of a RelativeLayout.  A relative layout will honor the size and pos of the child widget, where the pos of the child widget is relative to the enclosing RelativeLayout.  That did not seem require here, I replaced the RelativeLayout with a BoxLayout.

 

Hope this helps.  I’ve pasted your code below, and attached them in the zipfile.

 

from kivy.app import App
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import Screen
from kivy.uix.togglebutton import ToggleButton


class ExampleApp(App):
   
def build_config(self, config):
       
pass


class
HomeMenu(Screen):
   
pass


class
MyLayout(BoxLayout):
   
pass
   
# replaced this code with kv code
    # def __init__(self, **kwargs):
    #     super().__init__(**kwargs)
    #     self.add_widget(CategoryButton(gamename="somename", cat_type="category1"))


class CategoryWindow(Screen):
   
def reconstruct(self, category):  # created to call from new button
       
app = App.get_running_app()
        all_cat = app.root.get_screen(
'home_menu').ids.category_button.Allcategories
       
self.ids.dynamical_category.reconstruct('tic tac toe', all_cat[category])

   
# the code below looks sort of like what you have done in kv
    # def __init__(self, **kwargs):
    #     super().__init__(**kwargs)
    #     self.add_widget(RelativeLayout())
    #     self.sv = ScrollView()
    #     self.children[0].add_widget(self.sv)
    #     self.children[0].children[0].add_widget(DynamicalCategory())


class CategoryButton(Button):
    gamename = StringProperty()
    cat_type = StringProperty() 
# note type is a python builtin function

   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.text = 'Category Button'
       
self.size_hint = (1, None)
       
self.height = "80dp"
       
self.Allcategories = {"category1": ("cat1", "cat2", "cat3", "cat5", "cat9"),
                             
"category2": ("cat4", "cat5")}

   
def on_press(self):
        app = App.get_running_app()
        app.root.get_screen(
"category_window").ids.dynamical_category.reconstruct(minigame=self.gamename,
                                                                                 
categories=self.Allcategories[
                                                                                      
self.cat_type])
        app.root.current =
"category_window"


class DynamicalCategory(GridLayout):
   
def clear(self):  # created for new button
       
self.clear_widgets()

   
def reconstruct(self, minigame, categories):
       
self.clear_widgets()
       
for cat in categories:
           
self.add_widget(Catsbutton(text=cat, gamename=minigame))


class Catsbutton(ToggleButton):
    gamename = StringProperty()


if __name__ == '__main__':
    ExampleApp().run()

 

 

+++++++++++++++++++++  example.kv +++++++++++++++++++++++

 

#:import NoTransition kivy.uix.screenmanager.NoTransition

<HomeMenu>:
    MyLayout:
       
size_hint: 1,1
       
pos_hint: {"center_x" : 0.5, "center_y": 0.5}
        CategoryButton:
           
id: category_button
            gamename:
"somename"
           
cat_type: "category1"

<DynamicalCategory>:
   
size_hint_y: None
   
height: self.minimum_height
   
cols: 1

<Catsbutton>:
   
size_hint: 1, None
   
height: '80dp'

<CategoryWindow>: # a Screen
   
canvas.before:
       
Color:
            
rgba: 0, 1, 1, 1
       
Rectangle:
           
pos: self.pos
            size
: self.size
   
BoxLayout:
       
orientation: 'vertical'
       
BoxLayout:
           
size_hint_y: None
           
height: 48
           
Button:
               
id: back_button
               
text: "back"
               
on_press: root.manager.current = "home_menu"
           
Button:
               
text: 'Clear'
               
on_release: dynamical_category.clear()
           
Button:
               
text: 'Reconstruct Cat 1'
               
on_release: root.reconstruct('category1')
           
Button:
               
text: 'Reconstruct Cat 2'
               
on_release: root.reconstruct('category2')
       
ScrollView:
           
size: root.width, root.height - back_button.height
            size_hint
: 1, None
           
pos_hint: {"center_x" : 0.5, "center_y": 0.5}
           
canvas.before:
               
Color:
                   
rgba: 1, 1, 0, 1
               
Rectangle:
                   
pos: self.pos
                    size
: self.size
           
DynamicalCategory:
               
id:dynamical_category

ScreenManager# moved static code to kv
   
transition: NoTransition()
    HomeMenu:
        name:
'home_menu'
   
CategoryWindow:
        name:
"category_window"

The result is however multiple buttons stacked upon each other and the GridLayout is out of place. This might be due to the fact that the GridLayout is initialised before the kv file is read. Also the GridLayout seems to ignore everything done in the kivy file. How do i fix this, i.e how do i tell my Gridlayout (defined in the script) to follow the rules defined in the kv file or how do I update it in the script?

I'm a beginner at using GUI and help would be much Appreciated.

--
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/3fcd0942-0d79-4c6c-a6ab-a5e9180dfd3cn%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.

main.zip

Hacktorius BD

unread,
May 20, 2022, 12:23:57 PM5/20/22
to kivy-...@googlegroups.com
Can you tell me how to compress the kivy app size it's an issue for kivy building applications
a little app can be alike over 25 or 30 Mb? Can w compress the size of the application and like flutter or react native
It's a big issue  

Robert

unread,
May 20, 2022, 12:56:59 PM5/20/22
to Kivy users support
Is this an Android question?

Do a release build https://github.com/Android-for-Python/Android-for-Python-Users#release-builds and if applicable only build for one architecture.

But Python apps are never going to be the size of a native apps, because they include Python and all that implies.
Reply all
Reply to author
Forward
0 new messages