'Current Screen Size in Python3 with PyQt5

Is there an alternative in Qt5 python3 to the following code :

https://askubuntu.com/questions/153549/how-to-detect-a-computers-physical-screen-size-in-gtk

from gi.repository import Gdk
s = Gdk.Screen.get_default()
print(s.get_width(), s.get_height())


Solution 1:[1]

You can get the primary screen from the QApplication, which returns a QScreen object giving access to many useful properties:

import sys
from PyQt5 import QtWidgets

app = QtWidgets.QApplication(sys.argv)

screen = app.primaryScreen()
print('Screen: %s' % screen.name())
size = screen.size()
print('Size: %d x %d' % (size.width(), size.height()))
rect = screen.availableGeometry()
print('Available: %d x %d' % (rect.width(), rect.height()))

Note that the primaryScreen method is static, so if you've already created an instance of QApplication elsewhere in your application, can easily get a QScreen object later on like this:

screen = QApplication.primaryScreen()

Solution 2:[2]

The following python3 code allows to get screen size but is there an alternative to QtWidgets.QDesktopWidget().screenGeometry(-1) method :

import sys
from PyQt5 import QtWidgets

def main():
"""
allow you to get size of your current screen
-1 is to precise that it is the current screen
"""
    sizeObject = QtWidgets.QDesktopWidget().screenGeometry(-1)
    print(" Screen size : "  + str(sizeObject.height()) + "x"  + str(sizeObject.width()))   


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main()
    sys.exit(app.exec_())

Solution 3:[3]

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()))

Solution 4:[4]

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/

Solution 5:[5]

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.

Solution 6:[6]

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())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 M.Sarabi
Solution 3 eyllanesc
Solution 4 Megastar
Solution 5 davehill
Solution 6