• <tfoot id='Xo106'></tfoot>

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

    1. <legend id='Xo106'><style id='Xo106'><dir id='Xo106'><q id='Xo106'></q></dir></style></legend>
        <bdo id='Xo106'></bdo><ul id='Xo106'></ul>

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

        如何在 kivy-python 中使用具有多线程的时钟对象更新进度条?

        时间:2023-06-07
      1. <tfoot id='RIajy'></tfoot>

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

                <tbody id='RIajy'></tbody>
              <legend id='RIajy'><style id='RIajy'><dir id='RIajy'><q id='RIajy'></q></dir></style></legend>
              • <small id='RIajy'></small><noframes id='RIajy'>

                • <i id='RIajy'><tr id='RIajy'><dt id='RIajy'><q id='RIajy'><span id='RIajy'><b id='RIajy'><form id='RIajy'><ins id='RIajy'></ins><ul id='RIajy'></ul><sub id='RIajy'></sub></form><legend id='RIajy'></legend><bdo id='RIajy'><pre id='RIajy'><center id='RIajy'></center></pre></bdo></b><th id='RIajy'></th></span></q></dt></tr></i><div id='RIajy'><tfoot id='RIajy'></tfoot><dl id='RIajy'><fieldset id='RIajy'></fieldset></dl></div>
                  本文介绍了如何在 kivy-python 中使用具有多线程的时钟对象更新进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在 kivy 中制作一个应用程序,因为我正在使用 linux.我在使用 python-kivy 中的多线程时钟对象更新进度条 gui 时遇到问题.我正在使用这两个文件 .kv &.py 文件运行应用程序.

                  I am making an app in kivy for that i am using linux. i am getting a problem to update the progress bar gui using clock object with multithreading in python-kivy. i am using both files .kv & .py file run the app.

                  我的 main.py 文件:

                  my main.py file:

                  from kivy.app import App
                  from kivy.uix.label import Label
                  from kivy.uix.boxlayout import BoxLayout
                  from kivy.uix.progressbar import ProgressBar
                  from kivy.uix.button import Button
                  from kivy.uix.popup import Popup
                  from kivy.clock import Clock
                  import sys, threading, os, time
                  from socket import *
                  
                  class Out_Come(BoxLayout):
                  
                  
                      def Input_manager(self, *args):
                          try:
                              t = self.ids.nd.text
                              I=socket.gethostbyname(t)
                              st = self.ids.ti.text
                              et = self.ids.it.text
                              st1=int(st)
                              et1=int(et)
                              while st1<=et1:
                                  work=my_function(I,st1)
                                  work.start()
                                  Clock.schedule_once(lambda dt: work.run, 1)
                                  st1=st1+1
                                  self.ids.pb.value=self.ids.pb.value+1
                      except:
                          "Error"
                  class my_function(threading.Thread):
                      def __init__(self,n, d):
                          threading.Thread.__init__(self)
                          self.n = n
                              self.d = d
                      def run(self):
                          try:
                              get = socket(AF_INET, SOCK_STREAM)
                              get.settimeout(4)
                              get.connect((gethostbyname(self.n), int(self.d)))
                              print"done", self.d
                              get.close()
                          except:
                              print"not done", self.d
                  
                  class Example_app(App):
                  
                      def build(self):
                          return Out_Come()
                  
                  
                  if __name__ == '__main__':
                      Example_app().run()
                  

                  我的 .kv 文件:

                  <Out_Come>:
                      GridLayout:
                          orientation:'vertical'
                          rows:2
                          cols:2
                          padding:10
                          spacing:10
                          Label:
                              text:"[b]Enter value:[b]"
                              markup:True
                              size_hint:.3,.3
                          TextInput:
                              id:nd
                              text: ""
                              height:40 
                              multiline:False
                  
                          GridLayout:
                              orientation: 'vertical'
                              rows:2
                              cols:4
                              padding:10
                              spacing:10
                              Label:
                                  text:"[b]First:[b]"
                                  height:40
                                  size_hint:.25,.25
                                  markup:True
                              TextInput:
                                  id:ti
                                  text:""
                                  height: 40
                                  size_hint:.25,.25
                                  multiline:False
                              Label:
                                  text:"[b]last[b]"
                                  height:40
                                  size_hint:.25,.25
                                  markup:True
                              TextInput:
                                  id:it
                                  text:""
                                  height:40
                                  size_hint:.25,.25
                                  multiline:False
                  
                          GridLayout:
                              orientation: 'vertical'
                              rows:1
                              cols:2
                              padding:10
                              spacing:10
                              Button:
                                  id:get
                                  text:'Start'
                                  size_hint:.3,.3
                                  on_press: root.Input_manager()
                          ProgressBar:
                              id:pb
                              max:100
                              value:0
                  

                  进度条是否完美更新?我想在实时更新它.请告诉我执行此代码的更好方法.提前致谢.

                  Is the progress bar perfectly updating? i want upate it prefectly in live time. Please tell me the better way to execute this code. Thanks in advance.

                  推荐答案

                  你的代码有几个问题:

                  您的 UI 线程中有一个循环(这将停止 UI 更新)

                  You have a loop in your UI thread (which will stop the UI updating)

                          while st1<=et1:
                              work=my_function(I,st1)
                              work.start()
                              Clock.schedule_once(lambda dt: work.run, 1)
                              st1=st1+1
                              self.ids.pb.value=self.ids.pb.value+1
                  

                  您需要像这样将循环传递到您的线程中:

                  You need to pass the loop into your thread like this:

                     ...
                     def Input_manager(self, *args):
                          ...
                          st1=int(st)
                          et1=int(et)
                  
                          work=my_function(I,st1, est1,
                                           #sending a thread safe update progress bar function
                                           lambda : Clock.schedule_once(self.update_bar))
                          work.start()
                  
                     def update_bar(self, dt=None):
                        self.ids.pb.value=self.ids.pb.value+1
                  
                  class my_function(threading.Thread):
                      def __init__(self,n, st, et, update_bar):
                          threading.Thread.__init__(self)
                          self.n = n
                          self.st = st
                          self.et = et
                          self.update_bar = update_bar
                      def run(self):
                          for din range(self.st1, self.et1+1):
                              try:
                                  get = socket(AF_INET, SOCK_STREAM)
                                  get.settimeout(4)
                                  get.connect((gethostbyname(self.n), int(d)))
                                  print"done", d
                                  get.close()
                              except:
                                  print"not done", d
                              finally:
                                  self.update_bar() #this is our callback to update the bar!
                  

                  还有更多的修复需要完成(风格方面),但这超出了上下文

                  There are more fixes to be done (style wise) but that is outside the context

                  注意 - 这未经任何测试...

                  Note - this was not tested by any means...

                  这篇关于如何在 kivy-python 中使用具有多线程的时钟对象更新进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:kv 语言中的 Kivy 屏幕管理器参考 下一篇:获取kivy中选中复选框的值

                  相关文章

                • <small id='4V9Bi'></small><noframes id='4V9Bi'>

                • <tfoot id='4V9Bi'></tfoot>
                      <bdo id='4V9Bi'></bdo><ul id='4V9Bi'></ul>

                    1. <i id='4V9Bi'><tr id='4V9Bi'><dt id='4V9Bi'><q id='4V9Bi'><span id='4V9Bi'><b id='4V9Bi'><form id='4V9Bi'><ins id='4V9Bi'></ins><ul id='4V9Bi'></ul><sub id='4V9Bi'></sub></form><legend id='4V9Bi'></legend><bdo id='4V9Bi'><pre id='4V9Bi'><center id='4V9Bi'></center></pre></bdo></b><th id='4V9Bi'></th></span></q></dt></tr></i><div id='4V9Bi'><tfoot id='4V9Bi'></tfoot><dl id='4V9Bi'><fieldset id='4V9Bi'></fieldset></dl></div>
                      <legend id='4V9Bi'><style id='4V9Bi'><dir id='4V9Bi'><q id='4V9Bi'></q></dir></style></legend>