Current Screen Size in Python3 with PyQt5

Viewed 25541
6 Answers

The value for screenGeometry() is the display you take a look at. 0 is the main screen, 1, 2 indicates further monitors installed.

To list all availible displays:

def screen_resolutions():
    for displayNr in range(QtWidgets.QDesktopWidget().screenCount()):
        sizeObject = QtWidgets.QDesktopWidget().screenGeometry(displayNr)
        print("Display: " + str(displayNr) + " Screen size : " + str(sizeObject.height()) + "x" + str(sizeObject.width()))
from PyQt5.QtWidgets import QApplication, QWidget
self.desktop = QApplication.desktop()
self.screenRect = self.desktop.screenGeometry()
self.height = self.screenRect.height()
self.width = self.screenRect.width()

You can see here - https://www.programmersought.com/article/58831562998/

Screen information is available from the QApplication object as noted by ekhumoro. However, there should be only one global instance of QApplication. It is often set in the main script of a PyQt Gui application:

import sys
from PyQt5.QtWidgets import QApplication
def main():
...
   app = QApplication(sys.argv)
   window = MainWindow()
   window.show
    
   sys.exit(app.exec_())

That's probably not where you want to work with screen properties.

As pointed out by Omkar76, to access the global instance from elsewhere (e.g. in MainWindow()) use its instance() function. You can then use its primaryScreen property to get access to the many useful pieces of information available from the QScreen object:

from PyQt5.QtWidgets import QApplication, QMainWindow

class MainWindow(QMainWindow):
   def __init__(self):
       # Window dimensions
       app = QApplication.instance()
       screen = app.primaryScreen()
       geometry = screen.availableGeometry()
       self.setFixedSize(geometry.width() * 0.8, geometry.height() * 0.7)

QDesktopWidget is obsolete and so should be avoided.

i honestly don't know why it's always such a hustle to find pyqt5 answers,but anyway i think this is what you were looking for .

print(self.frameSize())
Related