Repeating texture class to inherit from

20 views
Skip to first unread message

Jakub Bláha

unread,
Jul 31, 2018, 3:09:23 PM7/31/18
to Kivy users support
Hi everyone, I need to create a class that I could inherit from and gain a repeating texture that way, which would then apply to the widget. I saw numerous posts about this topic, but none of the solutions worked for me. Here is my code:

from kivy.uix.image import CoreImage
from kivy.graphics import Rectangle

class NoiseTexture(Widget):
def __init__(self, **kw):
super().__init__(**kw)
with self.canvas.before:
tex = CoreImage('img/noise_texture.png').texture
tex.wrap = 'repeat'
Rectangle(pos=self.pos, size=self.size, texture=tex)

Jakub Bláha

unread,
Aug 2, 2018, 11:20:36 AM8/2/18
to Kivy users support
I have finally found a solution, here it is.


from kivy.uix.image import CoreImage
from kivy.graphics import Rectangle, Color
from kivy.properties import ListPropertyy
from kivy.clock import Clock


class NoiseTexture(Widget):
TEX_SIZE = 100, 100
noise_color = ListProperty((1, 1, 1, 1))

def __init__(self, **kw):
super().__init__(**kw)
Clock.schedule_once(lambda *args: Clock.schedule_once(self.update_texture))

def update_texture(self, *args):
tex = CoreImage('img/noise_texture.png').texture
tex.wrap = 'repeat'
tex.uvsize = self.width/self.TEX_SIZE[0], self.height/self.TEX_SIZE[1]
with self.canvas.before:
Color(rgba=self.noise_color)
Rectangle(pos=self.pos, size=self.size, texture=tex)

def on_noise_color(self, *args):
self.update_texture()

Reply all
Reply to author
Forward
0 new messages