<i id='BBiN1'><tr id='BBiN1'><dt id='BBiN1'><q id='BBiN1'><span id='BBiN1'><b id='BBiN1'><form id='BBiN1'><ins id='BBiN1'></ins><ul id='BBiN1'></ul><sub id='BBiN1'></sub></form><legend id='BBiN1'></legend><bdo id='BBiN1'><pre id='BBiN1'><center id='BBiN1'></center></pre></bdo></b><th id='BBiN1'></th></span></q></dt></tr></i><div id='BBiN1'><tfoot id='BBiN1'></tfoot><dl id='BBiN1'><fieldset id='BBiN1'></fieldset></dl></div>
      <bdo id='BBiN1'></bdo><ul id='BBiN1'></ul>

      <legend id='BBiN1'><style id='BBiN1'><dir id='BBiN1'><q id='BBiN1'></q></dir></style></legend>

        <small id='BBiN1'></small><noframes id='BBiN1'>

        <tfoot id='BBiN1'></tfoot>

        运行代码时更新 kivy 小部件的属性

        时间:2023-10-10
      1. <small id='vHjUx'></small><noframes id='vHjUx'>

                  <tbody id='vHjUx'></tbody>
                  <bdo id='vHjUx'></bdo><ul id='vHjUx'></ul>
                  <i id='vHjUx'><tr id='vHjUx'><dt id='vHjUx'><q id='vHjUx'><span id='vHjUx'><b id='vHjUx'><form id='vHjUx'><ins id='vHjUx'></ins><ul id='vHjUx'></ul><sub id='vHjUx'></sub></form><legend id='vHjUx'></legend><bdo id='vHjUx'><pre id='vHjUx'><center id='vHjUx'></center></pre></bdo></b><th id='vHjUx'></th></span></q></dt></tr></i><div id='vHjUx'><tfoot id='vHjUx'></tfoot><dl id='vHjUx'><fieldset id='vHjUx'></fieldset></dl></div>
                  <tfoot id='vHjUx'></tfoot>

                  <legend id='vHjUx'><style id='vHjUx'><dir id='vHjUx'><q id='vHjUx'></q></dir></style></legend>
                • 本文介绍了运行代码时更新 kivy 小部件的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想在运行某些东西时更新 kivy 小部件的属性...

                  I want to update the properties of a kivy widget while running something...

                  例子:

                  class app(App):
                      def build(self):
                          self.layout = Layout()
                          self.name = Label(text = "john")
                          self.layout.add_widget(self.name)
                          return self.layout
                  
                      def update(self):
                          for i in range(50): #keep showing the update
                              self.name.text = str(i)
                              #maybe some sleep here
                  
                  obj = app()
                  obj.run()
                  obj.update()
                  

                  这只会显示循环的最终结果.我想在循环进行时继续更新 label.text.

                  This is gonna show me only the final result of the loop. I'd like to keep updating the label.text while the loop goes.

                  我寻找了类似 bind()、setter() 和 ask_update() 函数,但如果是这些函数,我不知道如何使用它们.

                  I looked for something like the bind(), setter() and ask_update() functions, but if are these funcs, I didn't get how to use them.

                  ------------------ 编辑 -----------------------

                  ------------------ EDIT -----------------------

                  试图适应 inclement 答案(使用时钟在其他线程中运行更新函数),我得到下面的代码试图遵循我的问题的真实想法,但仍然无法正常工作:

                  Trying to adapt to inclement answer (running the update function in other thread using Clock), I got the code below trying to follow the real idea of my problem, but still not working:

                  class main():
                      def __init__(self, app):
                          self.app = app
                  
                      ... some code goes here ...
                  
                      def func(self):
                          Clock.schedule_once(partial(self.app.update, self.arg_1, self.arg_2), 0)
                  
                  class app(App):
                      def build(self):
                              self.main = main(self)
                              self.layout = Layout()
                              self.name = Label(text = "john")
                              self.layout.add_widget(self.name)
                              return self.layout
                  
                      ... some code goes here ...
                  
                      def update(self, dt, arg_1, arg_2):
                          self.name = arg_1
                          sleep(5)
                          self.name = arg_2
                  
                  obj = app()
                  obj.run()
                  

                  我需要调用 func 函数并使其在 update 函数中命令文本更改时准确地更新标签文本.

                  I need to call the funcfunction and make it update the label text exactly when I order the text change in update function.

                  推荐答案

                  你需要避免阻塞主线程.在大多数情况下,只使用 kivy 的时钟很方便.您可以执行以下操作.

                  You need to avoid blocking the main thread. In most cases, it's convenient to just use kivy's clock. You can do something like the following.

                  from kivy.clock import Clock
                  
                  class app(App):
                      def build(self):
                          self.layout = Layout()
                          self.name = Label(text = "john")
                          self.layout.add_widget(self.name)
                          self.current_i = 0
                          Clock.schedule_interval(self.update, 1)
                          return self.layout
                  
                      def update(self, *args):
                          self.name.text = str(self.current_i)
                          self.current_i += 1
                          if self.current_i >= 50:
                              Clock.unschedule(self.update)
                  

                  这篇关于运行代码时更新 kivy 小部件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python - Kivy:AttributeError:'super'对象在尝试获取 下一篇:ObjectProperty 类的使用

                  相关文章

                  最新文章

                • <i id='6nQ3L'><tr id='6nQ3L'><dt id='6nQ3L'><q id='6nQ3L'><span id='6nQ3L'><b id='6nQ3L'><form id='6nQ3L'><ins id='6nQ3L'></ins><ul id='6nQ3L'></ul><sub id='6nQ3L'></sub></form><legend id='6nQ3L'></legend><bdo id='6nQ3L'><pre id='6nQ3L'><center id='6nQ3L'></center></pre></bdo></b><th id='6nQ3L'></th></span></q></dt></tr></i><div id='6nQ3L'><tfoot id='6nQ3L'></tfoot><dl id='6nQ3L'><fieldset id='6nQ3L'></fieldset></dl></div>

                  <small id='6nQ3L'></small><noframes id='6nQ3L'>

                  1. <tfoot id='6nQ3L'></tfoot>
                    • <bdo id='6nQ3L'></bdo><ul id='6nQ3L'></ul>

                    1. <legend id='6nQ3L'><style id='6nQ3L'><dir id='6nQ3L'><q id='6nQ3L'></q></dir></style></legend>