Here is an example:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color, Rectangle
from kivy.clock import Clock
class FloatBox(FloatLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.draw_box()
def draw_box(self):
with self.canvas:
Color(rgba=(0, 1, 0, 1))
Rectangle(size=(200,200), pos=(0,0))
def clear_box(self):
self.canvas.clear()
class CanvasClearApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.show = True
self.float_box = None
def build(self):
self.float_box = FloatBox()
return self.float_box
def on_off(self, *args):
if self.show:
self.float_box.clear_box()
self.show = False
else:
self.float_box.draw_box()
self.show = True
def on_start(self):
Clock.schedule_interval(self.on_off, 1)
CanvasClearApp().run()
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/44560432-a6f3-4ffd-85e0-a1fba24715f8n%40googlegroups.com.
Here is an example:
Read: https://kivy.org/doc/master/api-kivy.clock.html?highlight=clock#module-kivy.clock
Clock.schedule_interval(self.on_off, 1) calls the method on_off(), once each second, causing a green square to appear and then disappear.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/e6da8d1f-b0ba-4afc-bff0-19ecd6d87e62n%40googlegroups.com.
Did you run the code I posted unmodified? This code runs on my machine.
What error message do you see?
If it is your code that is not working, post a minimal runnable example.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/39942a93-5f25-4e8e-b857-03f9e4a3cd86n%40googlegroups.com.
Share code I can run, a minimal program that shows the issue. When you are pasting code into the web site right click and select paste as plain text to retain formatting.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/3e57e3ef-c3bd-46a4-9353-2011485b015an%40googlegroups.com.
I don’t have keyboard installed, so I commented it out, and used a 2 second delay.
You were not seeing the screen clear – because in test the code calls draw_box() and displays the original image.
The code below will display an image, wait 2 seconds, then clear the screen. You will need to change the path to the image, I changed it to display an image I had on hand. I have highlighted changes below.
import os
from PIL import Image
# import keyboard
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.graphics import Rectangle
from kivy.uix.floatlayout import FloatLayout
class TSApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.title = "test"
# keyboard.add_hotkey("r", lambda: self.test())
Window.clearcolor = (0, 0, 0, 0)
Window.fullscreen = False
Window.size = (720, 720)
def build(self):
self.layout = FloatBox("ACESxp-30230 crop.jpg", (720, 720))
self.layout.draw_box()
return self.layout
def test(self, *args):
self.layout.clear_box()
if os.path.exists("randomimage.jpg"):
os.remove("randomimage.jpg")
img = Image.new("RGB", size=(720, 720))
img.save("randomimage.jpg"
)
# self.layout.draw_box() This displays the image again - so the screen never goes blank
def on_start(self):
Clock.schedule_once(self.test, 2)
class FloatBox(FloatLayout):
def __init__(self, file, size):
super().__init__()
self.file = file
self.size = size
def draw_box(self):
with self
.canvas:
Rectangle(pos=(0, 0), size=self.size, source=self.file)
# self.canvas.add(rect) Not required when using the context manager
def clear_box(self):
self.canvas.clear()
TSApp().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d65f9799-3907-42d2-8290-a148fa3d7402n%40googlegroups.com.
On Nov 25, 2021, at 12:11 PM, confusedtick 606 <tybikm...@gmail.com> wrote:
Now i m using two files and alternating them by this
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/3e45739a-6c75-4ca3-876f-54a31f8ac82fn%40googlegroups.com.
I’ve done a few experiments. It appears that the file name is being cached, and if it is not changed – it is not read. This requires the filename to be changed.
The Image class in kivy.image.uix has a nocache option, you could try using Image rather that the canvas.
FWIW here is my experiment with the filename changing….. this works. Tried a number of things but was not able to get this to work with the same filename.
import os
from PIL import Image
import shutil
# import keyboard
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.graphics import Rectangle
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import StringProperty, ListProperty
class TSApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.title = "test"
# keyboard.add_hotkey("r", lambda: self.test())
Window.clearcolor = (0, 0, 0, 0)
Window.fullscreen = False
Window.size = (720, 720
)
shutil.copy('ACESxp-30230 crop.jpg', 'randomimage.jpg')
def build(self):
self.layout = FloatBox(box_file="randomimage.jpg", box_size=(720, 720))
self.layout.draw_box()
return self.layout
def test(self, *args):
self.layout.clear_box()
# if os.path.exists("randomimage.jpg"):
# os.remove("randomimage.jpg")
# img = Image.new("RGB", size=(720, 720))
# img.save("randomimage_1.jpg")
self.layout.box_file = "randomimage_1.jpg" if self.layout.box_file == "randomimage.jpg" else "randomimage.jpg"
self.layout.draw_box()
def on_start(self):
Clock.schedule_interval(self.test, 2)
class FloatBox(FloatLayout):
box_file = StringProperty()
box_size = ListProperty()
def draw_box(self):
with self.canvas:
Rectangle(pos=(0, 0), size=self.box_size, source=self.box_file)
def clear_box(self):
self.canvas.clear()
TSApp().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/9ad75aa5-a6cf-41d6-b8bb-c0b6943aab2cn%40googlegroups.com.
When in doubt read the fine manual…. https://kivy.org/doc/master/api-kivy.resources.html#kivy.resources.resource_find
Files are cached by default and can be set to not cached.
Here is the experiment with Image, this worked. The source needed to be changed to force the file with the same name to be read again.
import os
from PIL import Image as PImage
import shutil
# import keyboard
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.uix.image import Image
from kivy.properties import StringProperty, ListProperty
class TSApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.title = "test"
# keyboard.add_hotkey("r", lambda: self.test())
Window.clearcolor = (0, 0, 0, 0)
Window.fullscreen = False
Window.size = (720, 720)
shutil.copy('ACESxp-30230 crop.jpg', 'randomimage.jpg')
def build(self
):
self.layout = FloatBox(box_file="randomimage.jpg", box_size=(720, 720), nocache=True) # used nocache option
self.layout.draw_box()
return self.layout
def test(self, *args):
# self.layout.clear_box()
if os.path.exists("randomimage.jpg"):
os.remove("randomimage.jpg")
img = PImage.new("RGB", size=(720, 720))
img.save("randomimage.jpg")
self.layout.draw_box()
def on_start(self):
Clock.schedule_interval(self.test, 2)
class FloatBox(Image):
box_file = StringProperty()
box_size = ListProperty()
def draw_box(self):
print('drawbox')
self.source = '' # need to change to source to force the file to be read again.
self.source = self.box_file
TSApp().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/61a05828.1c69fb81.9450c.d70bSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/MwuSwVNR4Rw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/61a05e45.1c69fb81.6eef6.8e82SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
Share your code. The code example below is working.
Or use the canvas in the layout, but turn off caching for the file… or use different file names for each image.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAORm2kCeSF7Sy8FF6gdFC%3DKtRmQ9vz-ZHfR6Bc%2BdQiC%2BOAt4qQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/61a139af.1c69fb81.c9ae1.c523SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
Yuck…. Good Luck!
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAORm2kC0Bo0wW05u5UQWt4hXng827cZDUThQ46cdafpY-0j-rQ%40mail.gmail.com.