Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill canvas with an image in kivy?

Tags:

python

kivy

how would I fill the entire rectangle with my image so it fits the whole screen as background?

from random import random
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line, Rectangle
from kivy.uix.filechooser import FileChooserListView, FileChooserIconView
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image

class MyPaintWidget(Widget):       
    def on_touch_down(self, touch):
        color = (random(), random(), random())
        with self.canvas:
            Rectangle(source='water.png', pos=(0, 0), size=self.size)        
            Color(*color)
            d = 30.
            touch.ud['line'] = Line(points=(touch.x, touch.y))

    def on_touch_move(self, touch):
        touch.ud['line'].points += [touch.x, touch.y]



class MyPaintApp(App):
    def build(self):
        parent = Widget()
        painter = MyPaintWidget()
        parent.add_widget(painter)


        return parent

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

right now it just displays a small image on the bottom left corner as default. I don't know how to resize or center the image because I don't know what the coordinates for the center are and the image does not resize.

like image 392
Cube_tbh Avatar asked Mar 16 '23 02:03

Cube_tbh


1 Answers

It's easy to write to .kv file.

<MyPaintWidget>
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'water.png'
like image 107
yuki Avatar answered Mar 24 '23 06:03

yuki