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.

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.
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.
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:
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/b7d88f32-42ed-4523-bee8-912756ee2359n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/6286d128.1c69fb81.7f4d3.df84SMTPIN_ADDED_MISSING%40gmr-mx.google.com.