How to add background image in Kivy without kv language - python

I'm creating an Kivy App for Desktop. I've created most of the app but I want to add a background image to the app. I've not use the KV language but created all the widgets using Python code only.
Can anybody please help me adding a background image in the kivy app using Python.

You can use with canvas: to draw a background image. Here is a simple example:
from kivy.app import App
from kivy.clock import Clock
from kivy.graphics.vertex_instructions import Rectangle
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
class TestApp(App):
def build(self):
theRoot = FloatLayout()
# draw the background
with theRoot.canvas:
self.rect = Rectangle(source='background.png')
# use binding to insure that the background stay matched to theRoot
theRoot.bind(on_size=self.update)
theRoot.add_widget(Label(text="Hi", size_hint=(None, None), size=(100, 50), pos=(100,100)))
# need to call update() to get background sized correctly at start
Clock.schedule_once(self.update, -1)
return theRoot
def update(self, *args):
# set the size and position of the background image
self.rect.size = self.root.size
self.rect.pos = self.root.pos
TestApp().run()

Related

kivy widgets doesn't appear just show black screen

I am a new learner of the Kivy module.I am practicing making a selfie app by following a video, by clicking on the button, the camera takes a frame and export it as a PNG image. in code I am adding widget (camera, button), and what show is a black screen. so what should I do?
import cv2
from kivy.app import App
from kivy.uix.camera import Camera
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class selfie_app(App):
def make(self):
self.obj_camera = Camera()
self.obj_camera.resolution = (810, 810)
obj_button = Button(text="Click !!")
obj_button.size_hint = (.5,.2)
obj_button.pos_hint = {'x':.15, 'y':.15}
obj_button.bind(on_press = self.selfie_take())
obj_layout = BoxLayout()
obj_layout.add_widget(obj_button)
obj_layout.add_widget(self.obj_camera)
return obj_layout
def selfie_take(self, *args):
print("selfie taken successfully !")
self.obj_camera.export_to_png('./demo_file.png')
if __name__ == '__main__':
selfie_app().run()

Kivy image not showing

Im working on a basic app using python and kivy and im trying to import an image which I'm basically going to use as a splash screen of sorts but for some reason I couldn't get it to work so I created a .py file and wrote some very simple code to try to get it to work:
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
label = Label(source="image.jpg")
return label
if __name__ == "__main__":
ImageTestApp().run()
The image is in the same directory of the .py file and the name of the image is exactly the same, I even made the window size the same as the image size and tried .PNG but nothing worked 😖
Label does not have an attribute called source. You should change Label to Image, and it will works. Please refer to example below for details.
Example
main.py
from kivy.app import App
from kivy.uix.image import Image
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
return Image(source="image.jpg")
if __name__ == "__main__":
ImageTestApp().run()
Output

How can I reload a image in kivy python

this is my script ...
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image
class MyApp(App):
def build(self):
return Image(source='go.jpg')
MyApp().run()
I won't to reload it becose the image is changing and I won't to see the new won all time
you can use reload() the read the image from the disk again
this will reload even if the original data of the image is updated or changed
self.ids.image1.source = './Images/file.png'
self.ids.image1.reload()
Make your own widget class, and make the image an attribute, so you can reference it. Then use clock to schedule an interval method, to constantly reload the image.
In the example below, the update_pic method is executed once every second.
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.uix.widget import Widget
class MyImageWidget(Widget):
def __init__(self,**kwargs):
super(MyImageWidget,self).__init__(**kwargs)
self.image = Image(source='go.jpg')
self.add_widget(self.image)
Clock.schedule_interval(self.update_pic,1)
def update_pic(self,dt):
self.image.reload()
class MyApp(App):
def build(self):
return MyImageWidget()
MyApp().run()
You can use the Image.reload method
def build(self):
img = Image(source='go.jpg')
Clock.schedule_interval(lambda dt: img.reload(), 0.2) #5 per second
return img

Scale python kivy line

I am trying to plot various line segments using kivy. The line appears really small (at one end of the screen) and I would like to scale it up. Ideally, I would like to specify coordinates from the center of screen and specify width and height so that the line segments appear well. Here is the code I wrote:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.graphics import Color, Ellipse, Line
class MyWidget(Widget):
def __init__(self, **kwargs):
super(MyWidget, self).__init__(**kwargs)
with self.canvas:
for obstacle in obstacles:
print obstacle
Line(points=[20, 5, 40, 5],width=1)
pass
# add your instruction for main canvas here
class MotionPlanningApp(App):
def build(self):
root = GridLayout(cols=1, padding=5, spacing=1)
root.add_widget(MyWidget())
return root
if __name__ == '__main__':
MotionPlanningApp().run()
Is there some way to do this in kivy?
You can use Translate instructions to move the line (effectively moving the origin of the canvas) and Scale instructions to stretch it.
For instance, you could have:
from kivy.core.window import Window
with self.canvas:
self.translate = Translate(Window.width / 2, Window.height / 2.)
You would also need to modify the translate instruction later if the window size changed, by binding a function to that.
Regardless, though, the coordinates are in screen pixels so the line is currently small just because you only made it 20 pixels long.

I want to dynamically change the color of the text in a widget in grid layout

I want to dynamically change the color of the text in a widget in grid layout.
How can i achieve that? I create a widget matrix of 6x6 and i need to blink the tile's text in the grid layout dynamically.
Its very same as your older question
I want to dynamically change the color of widget in grid Layout in kivy
just in place of background_color use color property of button . Try code below :
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.graphics import Color
from kivy.clock import Clock
import random
class RootWidget(GridLayout):
pass
class MainApp(App):
def build(self):
parent = GridLayout(cols=6)
for i in (1,2,3,4,5,6):
for j in (1,2,3,4,5,6):
parent.add_widget(Button(text='%s%s'%(i,j)))
Clock.schedule_interval(lambda a:self.update(parent),1)
return parent
def update(self,obj):
print "I am update function"
for child in obj.children:
c=[0,random.random(),1,random.random()]
child.color=c
if __name__ == '__main__':
MainApp().run()

Categories