How do I add buttons that are dynamically created in pure python to a kivy layout that is Written in Kivy Language?

Steve Hostetler

My problem is that I need to create a grid of buttons based on a variable number of grid squares, and place them on a grid layout and display them on a screen using the screen manager. I know how to do this in pure python using a simple for loop, but I wrote the layout for my program in kivy language, and I don't know how to add the buttons to the grid layout, because I don't know how to correctly reference them in the kv file. The relevant python code is:

def buildMap():
    index = 0
    for index in range(0, numberOfGridBlocks):
        mainMap.ids["Map"].add_widget(Button())
        index = index + 1
buildMap() 

The relevant part of the kv file is:

ScreenManagement:
    MainMenuScreen:
    NewGameMenuScreen:
    JoinGameMenuScreen:
    TutorialMenuScreen:
    SettingsMenuScreen:
    MapScreen:

<MenuButton>:
    on_press: app.menuButtonPressed()
    size_hint_y: .125
    background_normal: "images/button.png"
    background_down: "images/buttonPressed.png"

<Button>:

<BoxLayout>:
    orientation: "vertical"
<MapLayout>:

<MapScreen>:
    name: "mapScreen"
    MapLayout:
        id: "Map"
        cols: 5
jligeza

I hope this example makes it clear for you:

test.kv:

#:kivy 1.9.0
ScreenManager:
    MapScreen:

<MapScreen>:
    name: 'map'

    GridLayout:
        id: grid
        cols: 1

main.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.button import Button
from kivy.clock import mainthread

NUMBER_OF_BUTTONS = 5


class MapScreen(Screen):

    @mainthread
    def on_enter(self):
        for i in xrange(NUMBER_OF_BUTTONS):
            button = Button(text="B_" + str(i))
            self.ids.grid.add_widget(button)


class Test(App):
    pass


Test().run()

The @mainthead decorator is needed to slightly delay the function, so the kv file gets scanned first, making the ids list viable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically created kivy buttons run on_press and on_release immediately

From Dev

How do I dynamically add buttons using Java in a given XML layout file in android studio?

From Dev

Kivy putting buttons on the corner of layout

From Dev

How to add a stack layout for loop in Kivy?

From Dev

How do I use labels for a set of dynamically created radio buttons?

From Dev

How to center buttons in Kivy?

From Dev

How can I organise kivy layout?

From Dev

Python kivy placing buttons/labels in different places using float layout

From Dev

Kivy: how to use canvas for widgets created in python

From Dev

Python Kivy conditional Layout

From Dev

Python and Kivy language communication

From Dev

I want to dynamically change the color of widget in grid Layout in kivy

From Dev

How to change text of a label in the kivy language with python

From Dev

Kivy - Add an icon to tab buttons

From Dev

How do I set widget attributes by calling a function in Kivy Python?

From Dev

How do I install Kivy with Python 3.4 on Windows 8.1?

From Dev

How do i mask an image in kivy using python?

From Dev

Kivy add widget at the end of layout

From Dev

How do I add buttons with event handlers to a form dynamically?

From Dev

Kivy: Trying to get grid layout with buttons to scroll

From Dev

How do I set minimum allowable width/height for widget/layout in Kivy?

From Dev

How do I turn off fullscreen in Kivy?

From Dev

How do I install Kivy on Mac OS?

From Dev

How do I create a borderless window in Kivy?

From Dev

How do I initialise CheckBox state in kivy

From Dev

Kivy accessing dynamically created TextInput inside a GridLayout

From Dev

How do I add a LinearLayoutCompat view to an existing xml layout dynamically?

From Dev

Is there a simple way to add a border to Kivy Buttons

From Dev

Instantiation of Kivy Children Widgets between Python and Kivy Language

Related Related

  1. 1

    Dynamically created kivy buttons run on_press and on_release immediately

  2. 2

    How do I dynamically add buttons using Java in a given XML layout file in android studio?

  3. 3

    Kivy putting buttons on the corner of layout

  4. 4

    How to add a stack layout for loop in Kivy?

  5. 5

    How do I use labels for a set of dynamically created radio buttons?

  6. 6

    How to center buttons in Kivy?

  7. 7

    How can I organise kivy layout?

  8. 8

    Python kivy placing buttons/labels in different places using float layout

  9. 9

    Kivy: how to use canvas for widgets created in python

  10. 10

    Python Kivy conditional Layout

  11. 11

    Python and Kivy language communication

  12. 12

    I want to dynamically change the color of widget in grid Layout in kivy

  13. 13

    How to change text of a label in the kivy language with python

  14. 14

    Kivy - Add an icon to tab buttons

  15. 15

    How do I set widget attributes by calling a function in Kivy Python?

  16. 16

    How do I install Kivy with Python 3.4 on Windows 8.1?

  17. 17

    How do i mask an image in kivy using python?

  18. 18

    Kivy add widget at the end of layout

  19. 19

    How do I add buttons with event handlers to a form dynamically?

  20. 20

    Kivy: Trying to get grid layout with buttons to scroll

  21. 21

    How do I set minimum allowable width/height for widget/layout in Kivy?

  22. 22

    How do I turn off fullscreen in Kivy?

  23. 23

    How do I install Kivy on Mac OS?

  24. 24

    How do I create a borderless window in Kivy?

  25. 25

    How do I initialise CheckBox state in kivy

  26. 26

    Kivy accessing dynamically created TextInput inside a GridLayout

  27. 27

    How do I add a LinearLayoutCompat view to an existing xml layout dynamically?

  28. 28

    Is there a simple way to add a border to Kivy Buttons

  29. 29

    Instantiation of Kivy Children Widgets between Python and Kivy Language

HotTag

Archive