code: def scaleFitWindow(self):
          
          e = 2.0  # So that no scrollbars are generated.
          w1 = self.centralWidget().width() - e
          h1 = self.centralWidget().height() - e
          a1 = w1 / h1
          # Calculate a new scale value based on the pixmap's aspect ratio.
          w2 = self.canvas.pixmap.width() - 0.0
          h2 = self.canvas.pixmap.height() - 0.0
          a2 = w2 / h2
          return w1 / w2 if a2 >= a1 else h1 / h2
docs: Figure out the size of the pixmap in order to fit the main widget.