Estoy tratando de crear una pequeña GUI con kivy. al hacer clic en el botón llamado botón, debería abrir la ventana emergente en otro hilo con una barra de progreso que evoluciona después de 3 segundos. Pero kivy me da un error que dice "No se pueden crear instrucciones gráficas fuera del hilo principal de Kivy". ¿Cómo resolver este problema?
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.progressbar import ProgressBar
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.lang import Builder
import time
import threading
Builder.load_string("""
<Interface>:
orientation: 'vertical'
Label:
text: "Test"
BoxLayout:
orientation: 'vertical'
Label
text: "phone Number"
TextInput:
id: variable
hint_text: ""
Thebutton:
user_input: variable.text
text: "Buttion"
on_release: self.do_action()
""")
class Interface(BoxLayout):
pass
class Thebutton(Button):
def bar_de_progress(self):
bdp = ProgressBar()
poo = Popup(title="Brute Forcing...", content=bdp, size_hint=(0.5, 0.2))
poo.open()
time.sleep(1)
bdp.value = 25
def do_action(self, *args):
threading.Thread(target=self.bar_de_progress).start()
return
class MyApp(App, Thebutton):
def build(self):
return Interface()
if __name__ == "__main__":
MyApp().run()
Solución del problema
No puedo replicar su error, pero la regla general es no realizar operaciones de GUI en subprocesos que no sean el subproceso principal. Aquí hay una versión modificada de su Thebutton
clase que hace eso:
class Thebutton(Button):
def bar_de_progress(self):
# this is run on the main thread
self.bdp = ProgressBar()
poo = Popup(title="Brute Forcing...", content=self.bdp, size_hint=(0.5, 0.2))
poo.open()
threading.Thread(target=self.update_progress).start()
def update_progress(self):
# this is run on another thread
time.sleep(1)
self.bdp.value = 25
def do_action(self, *args):
self.bar_de_progress()
No hay comentarios:
Publicar un comentario