<small id='7vihL'></small><noframes id='7vihL'>

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

    1. <legend id='7vihL'><style id='7vihL'><dir id='7vihL'><q id='7vihL'></q></dir></style></legend>
        <bdo id='7vihL'></bdo><ul id='7vihL'></ul>
    2. <tfoot id='7vihL'></tfoot>
      1. Kivy:标签文本在 for 循环期间不更新

        时间:2023-06-07
        <legend id='lTzmg'><style id='lTzmg'><dir id='lTzmg'><q id='lTzmg'></q></dir></style></legend>

            <tfoot id='lTzmg'></tfoot>

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

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

                • 本文介绍了Kivy:标签文本在 for 循环期间不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 for 循环期间尝试更新标签文本时遇到问题.有类似的条目(例如:Update properties of a kivy widget while运行代码),但它们似乎并不完全适合我的问题(或者我错过了重点......).我运行以下代码:

                  I have an issue when I try to update a label text during a for-loop. There are similar entries (e.g.: Update properties of a kivy widget while running code) but they do not seem to fit my issue exactly (or I missed the point…). I run following code:

                  *.py:

                  from kivy.app import App
                  from kivy.uix.boxlayout import BoxLayout
                  from kivy.properties import StringProperty
                  #from time import sleep
                  
                  class MyBox(BoxLayout):
                      tobeupd = StringProperty()
                  
                      def __init__(self,*args,**kwargs):
                          super(MyBox,self).__init__(*args,**kwargs)
                          self.tobeupd = '#'
                  
                      def upd_ltxt(self):
                          for i in range(1,10):
                              self.tobeupd = str(i)
                              print(self.tobeupd)
                              input('Write something: ')  # new line, see edit below
                              #sleep(0.5) 
                  
                  class updApp(App):
                      def build(self):
                          return MyBox()
                  
                  if __name__ == '__main__':
                      updApp().run() 
                  

                  *.kv

                  <MyBox>:
                      orientation: 'horizontal'
                      cols: 2
                      Label:
                          text: root.tobeupd
                      Button:
                          text: 'Start Update'
                          on_release: root.upd_ltxt()
                  

                  虽然打印"语句会定期更新 shell,但标签文本仅在 for 循环结束时更新.谁能向我解释为什么 Kivy 会这样工作以及我如何克服这个问题?

                  Whereas the ‘print’ statement updates the shell regularly, the label text updates at the end of the for-loop only. Can anyone explain to me why Kivy works this way and how I can overcome this problem?

                  根据 PM2Ring 和 Gugas,我更改了代码以避免睡眠功能.如果我要求用户在循环继续之前输入一些内容,问题仍然存在.这些值在 shell 中更新,但不在标签上.

                  According to PM2Ring and Gugas, I changed the code in order to avoid the sleep-function. The problem remains if I ask the user to enter something before the loop can be continued. The values are updated in the shell but not on the label.

                  推荐答案

                  您可以为此使用 threading.
                  当您在 kivy 中执行循环或等待输入时,主线程正在等待,并且不会在应用程序上更新任何内容.threading 会阻止这种情况.
                  使用 threading 在主线程之外创建另一个线程.
                  示例:

                  You can use threading for this.
                  When you do a loop or wait for an input in kivy, the main thread is waiting, and nothing will update on the app. threading will prevent that.
                  Use threading to make another thread besides the main thread.
                  Example:

                  from kivy.app import App
                  from kivy.uix.boxlayout import BoxLayout
                  from kivy.properties import StringProperty
                  from kivy.lang import Builder
                  import threading
                  
                  Builder.load_string('''
                  
                  <MyBox>:
                      orientation: 'horizontal'
                      cols: 2
                      Label:
                          text: root.tobeupd
                      Button:
                          text: 'Start Update'
                          on_release: root.upd_ltxt()
                  
                  ''')
                  
                  class MyBox(BoxLayout):
                      tobeupd = StringProperty()
                  
                      def __init__(self,*args,**kwargs):
                          super(MyBox,self).__init__(*args,**kwargs)
                          self.tobeupd = '#'
                  
                      def upd_ltxt(self):
                          threading.Thread(target=self.update_label).start()
                  
                      def update_label(self):
                          for i in range(1,10):
                              print(self.tobeupd)
                              self.tobeupd = str(i)
                              input('Write something: ')  # new line, see edit below
                  
                  
                  
                  class updApp(App):
                      def build(self):
                          return MyBox()
                  
                  if __name__ == '__main__':
                      updApp().run()
                  

                  现在值得一提的是,即使第一个尚未完成,您也可以继续按下按钮并启动线程.这可能是不受欢迎的行为.
                  这可以通过在线程开始时禁用按钮并在结束时再次启用它来防止.

                  Now its worth mentioning that you can keep pushing the button and start threads, even if the first did not finish yet. This might be an unwanted behavior.
                  This can be prevented by disabling the button in the beginning of the thread, and enabling it again at the end.

                  在kv中给按钮一个id:

                  Give the button an id in kv:

                  Button:
                      id: updatebutton
                      text: 'Start Update'
                      on_release: root.upd_ltxt()
                  

                  在线程中这样做:

                  def update_label(self):
                  
                      self.ids.updatebutton.disabled = True
                  
                      for i in range(1,10):
                          self.tobeupd = str(i)
                          input('Write something: ')
                  
                      self.ids.updatebutton.disabled = False
                  

                  这篇关于Kivy:标签文本在 for 循环期间不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将功能绑定到 Kivy 按钮 下一篇:Python kivy - 如何减少 TextInput 的高度

                  相关文章

                • <small id='uYUXY'></small><noframes id='uYUXY'>

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

                    • <bdo id='uYUXY'></bdo><ul id='uYUXY'></ul>

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