0

I have this section of Python code which works correctly

   import mss
   import mss.tools
   import datetime
   import time

    with mss.mss() as sct:
    monitor = sct.monitors[1]
    timestr = time.strftime("%Y%m%d-%H%M%S")
    sct.compression_level = -1
    output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
    sct_img = sct.grab(monitor)
    mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
    print(output)

I would add a no end cycle/loop with a 10 second delay between each loop, so I added a while in this way

    import mss
    import mss.tools
    import datetime
    import time 

        while count < 100000000:

        with mss.mss() as sct:
        monitor = sct.monitors[1]
        timestr = time.strftime("%Y%m%d-%H%M%S")
        sct.compression_level = -1
        output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
        sct_img = sct.grab(monitor)
        mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
        print(output)    

        time.sleep(10)
        count += 1

but it returns this

IndentationError: unindent does not match any outer indentation level

How to add a loop in my code ?

2
  • You need indentation after with mss.mss() as sct: as well, so if this code is working you didn't post the actual code, and we can only guess (although a very good one) what is the problem. Commented Feb 6, 2020 at 6:30
  • there is no other code , I usually code with php and truly I do not understand this python logic. Commented Feb 6, 2020 at 6:35

3 Answers 3

1

there should be always an indent after the :

import mss
import mss.tools
import datetime
import time 

while count < 100000000:

    with mss.mss() as sct:
        monitor = sct.monitors[1]
        timestr = time.strftime("%Y%m%d-%H%M%S")
        sct.compression_level = -1
        output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
        sct_img = sct.grab(monitor)
        mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
        print(output)    

        time.sleep(10)
        count += 1
Sign up to request clarification or add additional context in comments.

1 Comment

is it not exactly my code above , I do not understand.
1

In Python there's no curly braces and code block start/end carried by indents, so you should indent with. It says that with is doing inside while.

while count < 100000000:
    with mss.mss() as sct:
        monitor = sct.monitors[1]
        timestr = time.strftime("%Y%m%d-%H%M%S")
        sct.compression_level = -1
        output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
        sct_img = sct.grab(monitor)
        mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
        print(output)    

    time.sleep(10)
    count += 1

Comments

1

There are several functions which require the indentation of the code that follows: for, while, if, with. The compiler (in python) needs the indentation to know with lines of code are triggered by which conditional statements. For instance, you want

    time.sleep(10)
    count += 1

to always execute during each iteration of while. Therefore the compiler needs to see:

while count < 100000000:
    with mss.mss() as sct:
        monitor = sct.monitors[1]
        timestr = time.strftime("%Y%m%d-%H%M%S")
        sct.compression_level = -1
        output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
        sct_img = sct.grab(monitor)
        mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
        print(output)    

    time.sleep(10)
    count += 1

Otherwise those two lines are only executed during the with portion of the loop.

3 Comments

is it not exactly what my code is doing ? Shame on me , I do not undestand .
The code you posted as working cannot work because some lines are only indented 1 space instead of the required 4 spaces.
I understood , so spaces are important and mandatory ! (Very different from php)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.