Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

python - How to add enumerate or a simple counter inside a message box

I developped a small application, but I would like to know how to add a simple counter inside a message box.

The message box, from QT designer, can appear 3 times. I would like to see the number in the message box.

Let's say it appears the first time, I should see -> 1, a second time, I should see -> 2, a third time -> 3.

Here is the part of the code:

            else:
                second_message_box(QMessageBox.Critical,
                                   "Attention!",
                                   "Il n'est pas possible d'ajouter L'inspecteur " + employeesChosen[i],
                                   QMessageBox.Ok)

I tried this, but it always gives me "1", it never iterate, even when the message box opens more than one time.

else:
    count = 0
    count += 1
    second_message_box(QMessageBox.Critical,
                       str(count),
                       "Il n'est pas possible d'ajouter L'inspecteur " + employeesChosen[i],
                       QMessageBox.Ok)

edit 3:

I did that

count = 0

def foo():
   ...

   else:
    global counter
    count += 1
    second_message_box(QMessageBox.Critical,
                       str(count),
                       "Il n'est pas possible d'ajouter L'inspecteur " + employeesChosen[i],
                       QMessageBox.Ok)
question from:https://stackoverflow.com/questions/65839344/how-to-add-enumerate-or-a-simple-counter-inside-a-message-box

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you can do is create a variable named count and modify that every time that else statement is called.

Here's an example:

def foo():
    count = 0

    for i in range(5):
       count += 1

The += operator increments the variable by the count of the integer on the right. If you happen to place the variable outside any function def blocks, be sure to indicate that the global variable of count needs to be modified.

count = 0

def foo():
   
   for i in range(5):
        global count
        count += 1

You can just print it after a certain line if you need to see it in the console.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...