How tor remove element from canvas.

386 views
Skip to first unread message

confusedtick 606

unread,
Nov 24, 2021, 1:28:05 PM11/24/21
to Kivy users support
Hello.
I m adding rectangle to layout canvas by this code:

rect = Rectangle(pos=(0, 0), size=((self.main.getMapManager().getMapSizeX() + 1) * 30, (self.main.getMapManager().getMapSizeY() + 1) * 30),source=self.main.getPath() + "\\" + "screen.jpg")

self.layout.canvas.add(self.rect)


but when i trying to remove this rectangle by this code:

self.layout.canvas.clear()

it didnt work 
(I m using FloatLayout)

Elliot Garbus

unread,
Nov 24, 2021, 5:19:31 PM11/24/21
to kivy-...@googlegroups.com

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.

 

confusedtick 606

unread,
Nov 25, 2021, 3:00:18 AM11/25/21
to Kivy users support
What is code for?

Clock.schedule_interval(self.on_off, 1) 

четверг, 25 ноября 2021 г. в 01:19:31 UTC+3, ElliotG:

confusedtick 606

unread,
Nov 25, 2021, 3:19:15 AM11/25/21
to Kivy users support
Still does not working

четверг, 25 ноября 2021 г. в 01:19:31 UTC+3, ElliotG:

Here is an example:

Elliot Garbus

unread,
Nov 25, 2021, 7:41:29 AM11/25/21
to kivy-...@googlegroups.com

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.

Elliot Garbus

unread,
Nov 25, 2021, 7:43:02 AM11/25/21
to kivy-...@googlegroups.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.

confusedtick 606

unread,
Nov 25, 2021, 9:03:13 AM11/25/21
to Kivy users support
FloatBox adding. but after reload method in did not changes.



I changed your class into this:

class FloatBox(FloatLayout):
def __init__(self, file, size):
super().__init__()
self.file = file
self.size = size

def draw_box(self):
with self.canvas:
rect = Rectangle(pos=(0, 0), size=self.size, source=self.file)
self.canvas.add(rect)

def clear_box(self):
self.canvas.clear()




Layout in main app build method: 

self.layout = FloatBox(self.file, self.size)




Reload method:

self.layout.clear_widgets()
self.layout.clear_box()

self.umap() #changing image in file. I v checked, it works (Pillow)

self.layout.draw_box() #image in file changed, but image on rectangle didnt





четверг, 25 ноября 2021 г. в 15:43:02 UTC+3, ElliotG:

confusedtick 606

unread,
Nov 25, 2021, 9:05:01 AM11/25/21
to Kivy users support
(I m not adding new files by umap(), I m just updating old)

четверг, 25 ноября 2021 г. в 17:03:13 UTC+3, confusedtick 606:

Elliot Garbus

unread,
Nov 25, 2021, 9:13:10 AM11/25/21
to kivy-...@googlegroups.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.

confusedtick 606

unread,
Nov 25, 2021, 9:53:54 AM11/25/21
to Kivy users support
This, hit r and image will be deleted, but draw_box() will show same pic


import kivy
import os
import keyboard
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.graphics import Rectangle
from PIL import Image
import time

class TSApp(App):

    def __init__(self):
        self.title = "test"
        keyboard.add_hotkey("r", lambda: self.test())
        Window.clearcolor = (0, 0, 0, 0)
        Window.fullscreen = False
        Window.size = (720, 720)
        App.__init__(self)

    def build(self):
        self.layout = FloatBox(r"C:\Users\defaultuser0\PycharmProjects\pythonProject3\randomimage.jpg", (720, 720))
        self.layout.draw_box()
        return self.layout

    def test(self):
        self.layout.clear_box()
        os.remove("randomimage.jpg")
        img = Image.new("RGB", size=(720, 720))
        img.save("randomimage.jpg")
        self.layout.draw_box()

class FloatBox(FloatLayout):
    def __init__(self, file, size):
        super().__init__()
        self.file = file
        self.size = size

    def draw_box(self):
        with self.canvas:
            rect = Rectangle(pos=(0, 0), size=self.size, source=self.file)
            self.canvas.add(rect)

    def clear_box(self):
        self.canvas.clear()


TSApp().run()
четверг, 25 ноября 2021 г. в 17:13:10 UTC+3, ElliotG:

Elliot Garbus

unread,
Nov 25, 2021, 10:16:30 AM11/25/21
to kivy-...@googlegroups.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()

confusedtick 606

unread,
Nov 25, 2021, 2:11:16 PM11/25/21
to Kivy users support
Now i m using two files and  alternating them by this

self.layout.file = self.main.getPath() + "\\" + self.scfile

But it still not working (I v checked, that my switching code is working fine)

And how do you highlight the text here?
четверг, 25 ноября 2021 г. в 18:16:30 UTC+3, ElliotG:

confusedtick 606

unread,
Nov 25, 2021, 2:18:21 PM11/25/21
to Kivy users support
I think you may not understand, but I just need to change the picture of this rectangle without changing the file. The picture is simply updated and is in the same file. Clearing the picture with pillow was just an example to show the problem.


четверг, 25 ноября 2021 г. в 22:11:16 UTC+3, confusedtick 606:

Elliot Garbus

unread,
Nov 25, 2021, 2:19:42 PM11/25/21
to kivy-...@googlegroups.com
I have the messages sent to my email, and highlight in the email client. 

If the examples I have sent you are not working perhaps you have an issue with your installation or your graphics driver.  Check the log for any error messages, update your graphics driver. 

Sent from my iPhone

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

Elliot Garbus

unread,
Nov 25, 2021, 10:44:46 PM11/25/21
to kivy-...@googlegroups.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()

Elliot Garbus

unread,
Nov 25, 2021, 11:10:49 PM11/25/21
to kivy-...@googlegroups.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()

confusedtick 606

unread,
Nov 26, 2021, 2:46:05 AM11/26/21
to kivy-...@googlegroups.com
I will use image widget. 

пт, 26 нояб. 2021 г. в 07:10, Elliot Garbus <elli...@cox.net>:
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.

confusedtick 606

unread,
Nov 26, 2021, 2:44:25 PM11/26/21
to kivy-...@googlegroups.com
Well, even with image widget my code does not working. Looks like img.reload() making screen black.

пт, 26 нояб. 2021 г. в 10:45, confusedtick 606 <tybikm...@gmail.com>:

Elliot Garbus

unread,
Nov 26, 2021, 2:46:59 PM11/26/21
to kivy-...@googlegroups.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.

confusedtick 606

unread,
Nov 26, 2021, 3:29:04 PM11/26/21
to kivy-...@googlegroups.com
I tried to change my videocard... 
Looks like it's almost died. Artefacts on screen. 

пт, 26 нояб. 2021 г., 22:47 Elliot Garbus <elli...@cox.net>:

Elliot Garbus

unread,
Nov 26, 2021, 3:32:27 PM11/26/21
to kivy-...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages