Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PyQt5的Python3中的当前屏幕大小_Qt_Python 3.x_Pyqt5 - Fatal编程技术网

PyQt5的Python3中的当前屏幕大小

PyQt5的Python3中的当前屏幕大小,qt,python-3.x,pyqt5,Qt,Python 3.x,Pyqt5,Qt5 python3中是否有以下代码的替代方案: 下面的python3代码允许获取屏幕大小,但是是否有替代方法 qtwidts.QDesktopWidget().screenGeometry(-1)方法: import sys from PyQt5 import QtWidgets def main(): """ allow you to get size of your courant screen -1 is to precise that it is the courant scre

Qt5 python3中是否有以下代码的替代方案:


下面的python3代码允许获取屏幕大小,但是是否有替代方法
qtwidts.QDesktopWidget().screenGeometry(-1)
方法:

import sys
from PyQt5 import QtWidgets

def main():
"""
allow you to get size of your courant screen
-1 is to precise that it is the courant 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_())
您可以从
QApplication
中获取,该应用程序返回一个允许访问许多有用属性的对象:

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()))
请注意,
primaryScreen
方法是静态的,因此,如果您已经在应用程序的其他位置创建了QApplication实例,以后可以很容易地获得QScreen对象,如下所示:

screen = QApplication.primaryScreen()

screenGeometry()
的值是您查看的显示。 0是主屏幕,1、2表示安装了更多监视器

要列出所有可用显示,请执行以下操作:

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

如ekhumoro所述,可从QApplication对象获取屏幕信息。但是,QApplication应该只有一个全局实例。它通常设置在PyQt Gui应用程序的主脚本中:

import sys
from PyQt5.QtWidgets import QApplication
def main():
...
   app = QApplication(sys.argv)
   window = MainWindow()
   window.show
    
   sys.exit(app.exec_())
这可能不是您想要处理屏幕属性的地方

正如Omkar76所指出的,要从其他地方(例如在MainWindow()中)访问全局实例,请使用其instance()函数。然后,可以使用其属性访问对象中的许多有用信息:


是过时的,因此应该避免。

太好了,您的代码允许我在屏幕上获得更多信息注意:您可以通过调用
qtwidts.QApplication.instance()
来获取当前应用程序实例。这对我很有用,因为我想在
QMainWindow
subclass@Omkar76我用一种更简单的方法更新了我的答案来获取屏幕对象。在Jupyter/JupyterLab中,我使用app=QtWidgets.QApplication([“”])为什么
screenGeometry(-1)
可以工作呢?上面的代码可以工作,但实际上并不明显……
screenGeometry(0)
更有意义(null QWidget),而且似乎也可以工作。很好的解决方案避免获得QApplication,谢谢!
import sys
from PyQt5.QtWidgets import QApplication
def main():
...
   app = QApplication(sys.argv)
   window = MainWindow()
   window.show
    
   sys.exit(app.exec_())
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)