Domkrt
Domkrt

Reputation: 85

Why two image (image.reload()) in kivy python?

I have kivy projekt and I make one image, but in the display is it 2. Why? I want update the image, and just one image display.

This is my code:

main.py

import numpy as np
import cv2
from PIL import Image as IMG
from PIL import ImageDraw, ImageFont
import random

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.uix.stacklayout import StackLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.slider import Slider
from kivy.uix.switch import Switch
from kivy.base import EventLoopBase
from kivy.clock import Clock
from functools import partial
from kivy.core.window import Window
Builder.load_file('kivy.kv')

def fotowrite(message):
    image = IMG.open('blue.jpg')

    font = ImageFont.truetype('Roboto.ttf', size=20) 
    draw = ImageDraw.Draw(image)
    #message = """    """ #datat of write
    color = 'rgb(0, 255, 0)' #rgb color
    draw.text((0, 0), message, fill=color, font=font) #erite
    image.save('blue2.jpg') 



class AlapWidget(StackLayout):
    pass

class GUIApp(App):
    def build (self):
        self.load_kv("kivy.kv")
        return AlapWidget()


def AppLoop(*args):
    win_ref = Window.get_parent_window().children[0]
    print('AppLoop')
    i=0
    i=random.random()
    fotowrite("value of i: "+str(i)) #here write to image, and save in HDD
    img = cv2.imread('blue2.jpg',cv2.IMREAD_UNCHANGED) 
    App.get_running_app().root.ids.imgm.reload()   
val1=None
Clock.schedule_interval(partial(AppLoop, 'val1'), 1)

GUIApp().run()

kivy.kv

<AlapWidget>:
    BoxLayout:                  
        orientation:'vertical'
        size_hint:0.55,0.45
        Image:
            id:imgm
            nocache:True
            source:'blue2.jpg' #load in disk
            size: self.texture_size
            allow_stretch:True
            keep_ratio:1

What is the problem? Why just the second reloaded?

Sorry, I speak a little English, and I'm beginner in GUI.

Thanks

Upvotes: 0

Views: 142

Answers (1)

John Anderson
John Anderson

Reputation: 39082

The problem is that you are loading the kivy.kv file twice. Once at the line:

Builder.load_file('kivy.kv')

and again in your GUIApp at the line:

self.load_kv("kivy.kv")

The kv file should only be loaded once. The App method (load_kv()) is normally called automatically by the App, and by default it looks for a kv file with a name based on the App name. So, in your case, if you change the name of your kivy.kv to gui.kv, then it will be loaded automatically, and you can eliminate both of those kv loading calls. If you prefer to keep the name kivy.kv then just eliminate the self.load_kv("kivy.kv") call.

Upvotes: 1

Related Questions