본문 바로가기

PROGRAM/PyQt5 with Python

다중모니터 활성화된 화면 정보 얻기

Pyqt5에서 designer를 사용하여 테스트 중

1. 주모니터가 1인경우

모든 모니터 정보를 출력하면  > 주 모니터 정보를 먼저 출력함 

 - DISPLAY1 => DISPLAY2 순임

- 모니터1이 기준으므로 왼쪽의 모니터2는 마이너스 좌표임

from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QDialog, QWidget, QDesktopWidget
from PyQt5.QtCore import Qt, QPoint
import sys
from PyQt5 import uic

form_sub = uic.loadUiType('qwin_v4_sub.ui')[0]
class myMain(QMainWindow, form_sub):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.show()

    def initUI(self):
        self.setupUi(self)
        self.getMonitorInfo()
        self.center()

    def getMonitorInfo(self):
        # app = QApplication([])

        # 현재 활성화된 모니터 정보 얻기
        desktop = app.desktop()
        current_screen = desktop.screenNumber(self)

        screens = app.screens()
        current_screen_name = screens[current_screen].name()
        print("Current screen:", current_screen_name)

        # 모든 모니터 정보 얻기
        for screen in screens:
            print("Name:", screen.name())
            print("Geometry:", screen.geometry())
            print("Available Geometry:", screen.availableGeometry())

        # 현재 시스템 정보 가져오기
        screen_count = desktop.screenCount()   # 듀얼모니터 개수
        print('total screens :', screen_count)
        # 중앙 좌표 구하기
        active_screen = desktop.screenGeometry()
        center_point = active_screen.center()

        # 중앙 좌표 출력하기
        self.lbl_info.setText(f"Active screen center point: ({center_point.x()}, {center_point.y()})")

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = myMain()
    sys.exit(app.exec_())

 

2. 주모니터가 2인경우

모든 모니터 정보를 출력하면  > 주 모니터 정보를 먼저 출력함

 - DISPLAY2 => DISPLAY1 순임

- 왼쪽의 모니터2가 주모니터이므로 오른쪽 모니터1은 1920으로 시작함

- 주모니터(0~1919) 서브모니터(1920~3839)

- QRect는 ( x, y , width, height) 임

- 메뉴창의 세로길이가 40임

from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QDialog, QWidget, QDesktopWidget
from PyQt5.QtCore import Qt, QPoint
import sys
from PyQt5 import uic

form_sub = uic.loadUiType('qwin_v4_sub.ui')[0]
class myMain(QMainWindow, form_sub):
    def __init__(self    ):
        super().__init__()
        self.initUI()
        self.show()

    def initUI(self):
        self.setupUi(self)
        self.getMonitorInfo()
        self.center()

    def getMonitorInfo(self):
        # app = QApplication([]) # 아래에 동일한 코드가 있어 문제가 계속 발생

        # 현재 활성화된 모니터 정보 얻기
        desktop = app.desktop()
        current_screen = desktop.screenNumber(self)

        screens = app.screens()
        current_screen_name = screens[current_screen].name()
        print("Current screen:", current_screen_name)

        # 모든 모니터 정보 얻기
        for screen in screens:
            print("Name:", screen.name())
            print("Geometry:", screen.geometry())
            print("Available Geometry:", screen.availableGeometry())

        # 현재 시스템 정보 가져오기
        screen_count = desktop.screenCount()   # 듀얼모니터 개수
        print('total screens :', screen_count)
        # 중앙 좌표 구하기
        active_screen = desktop.screenGeometry()
        self.center_point = active_screen.center()

        # 중앙 좌표 출력하기
        self.lbl_info.setText(f"Active screen center point: ({self.center_point.x()}, {self.center_point.y()})")

    def center(self):
        qr = self.frameGeometry()
        # cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(self.center_point)
        self.move(qr.topLeft())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = myMain()
    sys.exit(app.exec_())