How to create a Progressbar with Python and Pyqt5?

Asked

Viewed 203 times

-1

I’m having a little trouble creating a Progress bar, it’s actually about interacting with her because creating in yourself is actually easy:

self.progressbar = QProgressBar()
self.progressBar.setRange(0, 10000)
self.progressBar.setValue(0)

With this it already appears. My difficulty is the following:

I have a variable that is the maximum amount of files I want to copy, I believe that would be the maximum value of mine progressbar. The initial value would be zero. Now, how would you make this iteration to fill the progressbar, showing the percentage?

  • I would like to know who is the great genius who likes to give a negative signal without saying what is wrong so that we can improve. Where is the moderator that at this time does not help?

  • 2

    If you have N files in total and already copied n, your percentage will be (n/N)*100.

  • @Andersoncarloswoss or just set the maximum range value and in setValue apply which is the current index :)

  • @Guilhermenascimento So he would show equal percentage?

  • @Andersoncarloswoss the bar yes, if there is text no. In the case of "text/label" I agree with you, it will have to be in the "rule of three"

  • Dear Cleber, the downvote is not mine, but when you receive one it is no use to think it is personal, I will say what your question probably has problem: You did not present the code that copies, so it is difficult to orient. So whoever voted negative was a way of calling attention by saying that the question this vague, I took a risk in answering, but often this causes more work for those who answer than they should, I’m not saying that is your case, but in most cases yes.

  • Thank you Guilherme Nascimento. That’s what I mean. For many like me who is new here, we need help instead of just negatively or criticize. With this I am improving my posts.

Show 2 more comments

1 answer

3


You can do it in two ways, set the range with the amount of files (only at the beginning, you do not need to stay within the "operation" of copies):

self.progressBar.setRange(0, quantidadeDeArquivos)

And every search for a new copied file tell what value is the index:

self.progressBar.setValue(indexAtual)

So you don’t need to calculate the percentage

The other way is just calculate the percentage, arrow the range as 100:

self.progressbar = QProgressBar()
self.progressBar.setRange(0, 100)

And in the value apply the calculation in setValue:

self.progressBar.setValue((indexAtual / quantidadeDeArquivos) * 100)

But really this is not necessary, since you can control the range limit, unless the intention is to show "no label" in percentage as well

Browser other questions tagged

You are not signed in. Login or sign up in order to post.