How do I add buttons that are dynamically created in pure python to a kivy layout that is Written in Kivy Language? -
my problem need create grid of buttons based on variable number of grid squares, , place them on grid layout , display them on screen using screen manager. know how in pure python using simple loop, wrote layout program in kivy language, , don't know how add buttons grid layout, because don't know how correctly reference them in kv file. relevant python code is:
def buildmap(): index = 0 index in range(0, numberofgridblocks): mainmap.ids["map"].add_widget(button()) index = index + 1 buildmap()
the relevant part of 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
i hope example makes clear 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 -*- kivy.app import app kivy.uix.screenmanager import screen kivy.uix.button import button kivy.clock import mainthread number_of_buttons = 5 class mapscreen(screen): @mainthread def on_enter(self): 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 needed delay function, kv file gets scanned first, making ids
list viable.
Comments
Post a Comment