How can I reload a image in kivy python - 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

Related

os.system() don't let updating image source in kivy python

I am trying to update the image source on a changeImageSource function it changes the source instantly but when I use time.sleep() method in that function, function executes but doesn't update the source of the image. updates after time.sleep() call completed.
from kivy.app import App
from kivy.uix.image import AsyncImage
from kivy.uix.button import Button
from kivy.uix.widget import Widget
import time
# creating the App class
class MyApp(App):
def build(self):
parent = Widget()
#creating and adding image to widget
self.img = AsyncImage(
source='http://kivy.org/logos/kivy-logo-black-64.png')
self.img.pos = (400,400)
#creating btn and adding press handler
self.change_img_btn = Button(text="Change Image ")
self.change_img_btn.bind(on_press = self.changeImageSource)
#adding widget to Widget instance
parent.add_widget(self.img)
parent.add_widget(self.change_img_btn)
return parent;
def changeImageSource(self,*args):
self.img.source = "https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a"
time.sleep(4)
# run the App
MyApp().run()
One: The "return parent;" should be "return parent"
Two: Why do you need time.sleep()?
You can also try flipping line 29 and 30.

Boxlayout is not showing button on the screen

getting the blank screen after running this code what is the problem
after adding .kv file everything is working fine and after removing .kv file and using the only python to show the layout it is not working
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class BoxLayoutExample(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
b1 = Button(text="A")
b2 = Button(text="B")
b3 = Button(text="C")
self.clear_widgets()
self.add_widget(b1)
self.add_widget(b2)
self.add_widget(b3)
class MainWidget(Widget):
pass
class TheLabApp(App):
pass
TheLabApp().run()
This the output:)
Image
If you are not using a kv file named thelab.kv, then your App has no way of knowing what you want it to look like. You will at least need to add a build() method to your TheLabApp class that returns BoxLayoutExample().

Image not showing when created from a thread kivy/kivymd

I'm working on an app, and I need the images to display independently at a specific timing. I have set up a thread using python's stock threading module, it runs and works normally instead of the image it displays a black square. Does anyone know how to fix it?
Here is my code to reproduce the issue:
import threading
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
class TestApp(App):
def build(self):
self.fl = FloatLayout()
self.fl.add_widget(Button(text="show image", on_press=self.start_thread))
return self.fl
def insertfunc(self):
self.fl.add_widget(Image(source="HeartIcon.png"))
def start_thread(self, instance):
threading.Thread(target=self.insertfunc).start()
TestApp().run()
Any help will be appreciated!
The add_widget() must be done on the main thread. I assume that you are using threading because you have additional things to do on the Thread aside from just the add_widget(). Based on that assumption, here is a modified version of your code that does what I think you want:
import threading
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
class TestApp(App):
def build(self):
self.fl = FloatLayout()
self.fl.add_widget(Button(text="show image", on_press=self.start_thread))
return self.fl
def insert_image(self, dt):
self.fl.add_widget(Image(source="HeartIcon.png"))
def insertfunc(self):
# do some calculations here
Clock.schedule_once(self.insert_image)
def start_thread(self, instance):
threading.Thread(target=self.insertfunc).start()
TestApp().run()
If you are not doing anything else in the new thread, then you don't actually need another thread. The start_thread() method can just do the:
self.fl.add_widget(Image(source="HeartIcon.png"))

First character not getting printed in kivy

I am learning to code in Kivy using python
But my Label is not showing the first character
Can anyone help me
The kv File is as follows:
<MyGrid>
Label:
text: "Techy Matanhelia"
The python file is as Follows:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.widget import Widget
class MyGrid(Widget):
pass
class MyApp(App):
def build(self):
return MyGrid()
if __name__ == '__main__':
MyApp().run()
Also Can anyone tell me how to edit kivy files on pycharm2020.2.2
That is because your MyGrid class extends Widget. A simple Widget has no capability to handle positioning or sizing of its children, so the Label gets assigned the default size of (100,100) and the default position of (0,0). which results in the first letter of the Label being off the screen. Try making MyGrid extend a Layout, perhaps like this:
class MyGrid(FloatLayout):
pass

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

Categories