如何打破循环?

2024-06-25 23:27:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在检查进度条的值,如果它是100,我想退出,这样完成的上传不会被打印两次。如果我查询进度条,我不知道为什么100会出现两次。在

def callback(self,*args):
  cmds.progressBar('progbar', edit=True, step=1)
  if cmds.progressBar('progbar',q=True, progress=True)==100:
    print "Finished Uploading."
    break

我已经找到了a similar thread,但在我的情况下,情况略有不同。。。在


Tags: 进度条selftrueifdefstepcallback情况
1条回答
网友
1楼 · 发布于 2024-06-25 23:27:49

您没有提供足够的代码示例来帮助您。首先,callback()函数包含一个break语句,即使这个函数中实际上没有循环。在

在任何情况下,我编写了下面的示例进行检查,它实际上只打印“完成上传”一次,当达到100时。在

import maya.cmds as cmds

cmds.progressBar('progressBar') # initialise the progress bar

def callback():  
  # Begin loop from 0 to 100 - remember range(n) returns numbers from 0 (inclusive) to n (exclusive)
  for i in range(101):
      cmds.progressBar('progressBar', edit=True, step=1) # increase step by 1
      if (cmds.progressBar('progressBar',q=True, progress=True)==100):
          print "Finished Uploading."
          break
          # Theoretically, breaking is simply here so that you stop wasting iterations in some cases,
          # but it's not necessary for the printout, since we have only specified that the print will
          # happen when the progress value is equal to exactly 100.
          # Finally, if there is NO loop (like the code you pasted originally), then you shouldn't have
          # a break.

callback()

出现问题的一个可能原因是,进度条的进度超过了100步,进度条的最大值是100。在这种情况下,随着步长的增加,值将保持在100(这是最大值),因此每次查询它是否等于100时,它都将返回True。在

不幸的是,如果你没有更具体的信息,我无法回答任何更具体的问题。在

相关问题 更多 >