无法在 Kivy 主线程之外创建图形指令

Rak*_*oob 5 python kivy

我正在尝试用 kivy 创建一个小型 GUI。通过单击名为“button”的按钮,它应该在另一个线程上启动弹出窗口,并在 3 秒后显示进度条。但是 kivy 给我一个错误,说“无法在主 Kivy 线程之外创建图形指令”如何解决这个问题?

 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()
Run Code Online (Sandbox Code Playgroud)

Joh*_*son 0

我无法复制您的错误,但一般规则是不要在主线程以外的线程上执行 GUI 操作。这是您的类的修改版本Thebutton,可以执行此操作:

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()
Run Code Online (Sandbox Code Playgroud)