Я новичок в python и kivy, и у меня возникла следующая проблема.
У меня есть этот код в моем main.kv
<FirstScreen>:
image_step: image_step.__self__
lblTest: lblTest
BoxLayout:
orientation: "vertical"
BoxLayout:
size_hint: [1,3]
CheckBox:
on_active: root.task_checked_event(self, self.active)
size_hint: [0.2, 0.2]
pos_hint: {'y': 0.35}
Label:
id: lblTest
text: "Check if completed"
size_hint: [0.5, 0.5]
pos_hint: {'y': 0.2}
AsyncImage:
id: image_step
size_hint: [1, 1]
BoxLayout:
spacing: 20
padding: [10, 10]
Button:
text: "Previous"
size_hint: [0.5, 0.5]
on_release: root.previous_step_event()
Button:
text: "Next"
size_hint: [0.5, 0.5]
on_release: root.next_step_event()
Тогда мой код main.py в kivy
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty, StringProperty
from kivy.core.window import Window
from kivy.utils import get_color_from_hex
from service_thread import Services
from signal_senders import SignalSenders
from pydispatch import dispatcher
import time
import sys
import threading
#Hex convertido desde RGB en la pagina:
#https://www.rapidtables.com/convert/color/rgb-to-hex.html
Window.clearcolor = get_color_from_hex('000000')
class FirstScreen(Screen):
image_step = ObjectProperty(None)
#relative_path = StringProperty('avatar.jpg')
#lblTest = ObjectProperty(None)
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
self.sender_signals_props = SignalSenders() #Objeto para acceder a sus atributos senders y signals para pyDispatcher
dispatcher.connect(self.update_gui,
signal=self.sender_signals_props.thread_signal,
sender=self.sender_signals_props.thread_sender)
dispatcher.connect(self.file_path_received_event,
signal=self.sender_signals_props.thread_filepath_signal,
sender=self.sender_signals_props.thread_filepath_sender)
self.step_number = 1
self.step_word = "step"
self.steps_images_dictionary = {'test': 'avatar.jpg'}
#self.image_step.source = self.steps_images_dictionary['test'] #Inicializo la primera imagen con el elemento 1 del diccionario.
self.image_step.source = self.steps_images_dictionary['test']
self.services = threading.Thread(target=Services)
self.services.start() #Iniciar el background task! :D
def update_gui(self, message=""):
pass
def file_path_received_event(self, event_args):
#Recibir el path relativo de la imagen
key_word = f"{self.step_word}{self.step_number}"
self.steps_images_dictionary[key_word] = event_args
print(f"Step number: {self.step_number}\n{self.steps_images_dictionary}")
#self.image_step.source = "./Fotos/area1/meliodas1.jpg" #self.steps_images_dictionary[key_word]
print("Antes de actualizar la imagen!")
self.image_step.source = "./Fotos/area1/meliodas1.jpg" #Try 1
#self.ids.image_step.source = "./Fotos/area1/meliodas1.jpg" #Try 2
print("Despues de actualizar la imagen!")
self.step_number += 1
def task_checked_event(self, instance, active):
#self.stop_service = active
dispatcher.send(message=active,
signal=self.sender_signals_props.main_signal,
sender=self.sender_signals_props.main_sender)
def next_step_event(self):
try:
pass
except:
print(f"Something went wrong: {sys.exc_info()[0]}")
def previous_step_event(self):
try:
pass
except:
print(f"Something went wrong: {sys.exc_info()[0]}")
class MainApp(App):
def build(self):
return FirstScreen()
MainApp().run()
У меня есть pydispatcher в другом потоке, поднимающий file_path_received_event, который отправляет строку локального пути, в котором я сохранил файл. Когда я пытаюсь "Refre sh" изображение, которое я сейчас показываю в gui, я получаю следующую ошибку:
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [Text ] Provider: sdl2
[INFO ] [Loader ] using a thread pool of 2 workers
[INFO ] [Base ] Start application main loop
[INFO ] [GL ] NPOT texture support is available
Step number: 1
{'test': 'avatar.jpg', 'step1': './Fotos/area1/meliodas1.jpg'}
Antes de actualizar la imagen!
Despues de actualizar la imagen!
Comenzando a contar
[INFO ] [Base ] Leaving application in progress...
Traceback (most recent call last):
File "C:\Users\52662\Desktop\Proyectos\DELTA\IoT\RaspberryPi\User_Interface\User_Interface\main.py", line 105, in <module>
MainApp().run()
File "C:\Users\52662\Anaconda3\lib\site-packages\kivy\app.py", line 855, in run
runTouchApp()
File "C:\Users\52662\Anaconda3\lib\site-packages\kivy\base.py", line 504, in runTouchApp
EventLoop.window.mainloop()
File "C:\Users\52662\Anaconda3\lib\site-packages\kivy\core\window\window_sdl2.py", line 747, in mainloop
self._mainloop()
File "C:\Users\52662\Anaconda3\lib\site-packages\kivy\core\window\window_sdl2.py", line 479, in _mainloop
EventLoop.idle()
File "C:\Users\52662\Anaconda3\lib\site-packages\kivy\base.py", line 339, in idle
Clock.tick()
File "C:\Users\52662\Anaconda3\lib\site-packages\kivy\clock.py", line 591, in tick
self._process_events()
File "kivy\_clock.pyx", line 384, in kivy._clock.CyClockBase._process_events
File "kivy\_clock.pyx", line 414, in kivy._clock.CyClockBase._process_events
File "kivy\_clock.pyx", line 412, in kivy._clock.CyClockBase._process_events
File "kivy\_clock.pyx", line 154, in kivy._clock.ClockEvent.tick
File "kivy\_clock.pyx", line 86, in kivy._clock.ClockEvent.get_callback
File "C:\Users\52662\Anaconda3\lib\site-packages\kivy\weakmethod.py", line 56, in is_dead
return self.proxy is not None and not bool(dir(self.proxy))
ReferenceError: weakly-referenced object no longer exists
Despues de contar
Step number: 2
{'test': 'avatar.jpg', 'step1': './Fotos/area1/meliodas1.jpg', 'step2': './Fotos/area1/meliodas2.jpg'}
Antes de actualizar la imagen!
Despues de actualizar la imagen!
Comenzando a contar
Despues de contar
Step number: 3
{'test': 'avatar.jpg', 'step1': './Fotos/area1/meliodas1.jpg', 'step2': './Fotos/area1/meliodas2.jpg', 'step3': './Fotos/area1/meliodas3.jpg'}
Antes de actualizar la imagen!
Despues de actualizar la imagen!
Comenzando a contar
Despues de contar
Step number: 4
{'test': 'avatar.jpg', 'step1': './Fotos/area1/meliodas1.jpg', 'step2': './Fotos/area1/meliodas2.jpg', 'step3': './Fotos/area1/meliodas3.jpg', 'step4': './Fotos/area1/meliodas4.jpg'}
Antes de actualizar la imagen!
Despues de actualizar la imagen!
Comenzando a contar
Despues de contar
Step number: 5
{'test': 'avatar.jpg', 'step1': './Fotos/area1/meliodas1.jpg', 'step2': './Fotos/area1/meliodas2.jpg', 'step3': './Fotos/area1/meliodas3.jpg', 'step4': './Fotos/area1/meliodas4.jpg', 'step5': './Fotos/area1/meliodas5.jpg'}
Antes de actualizar la imagen!
Despues de actualizar la imagen!
Comenzando a contar
Despues de contar
{'area': 'area1', 'files': {'image1': 'meliodas1.jpg', 'image2': 'meliodas2.jpg', 'image3': 'meliodas3.jpg', 'image4': 'meliodas4.jpg', 'image5': 'meliodas5.jpg'}}
Services Thread Ended!
Я использую python 3.8, Windows 10, IDE Spyder . Если необходимо разместить изображения, я могу это сделать, без проблем. Может ли кто-нибудь помочь мне с этим?