python/기타

Split The Screen - 창 분할 프로그램

zoomer75 2021. 11. 24. 15:09

Split The Scree

최근에 그램 17인치를 사면서 2560 해상도의 광할한 화면을 즐기고 있습니다.

듀얼모니터가 없이도 왼쪽에는 브라우저 오른쪽에는 vscode 를 쓰고 있습니다.

그런데 창을 새로 연다던지 왼쪽 오른쪽 이동하다 보면 창 정렬이 안 맞는 경우가 있습니다.

기존에 power toys 를 쓰고 있어서 창을 5:5 로 분할하여 쓰고 있으나 상활에 따라 3:7 등으로 자유롭게 변환하기에는 좀 번거롭더군요.

그래서 창 분할 프로그램을 하나 자작했습니다.

화면 처럼 슬라이드를 통해 원하는 비율을 선택하면 비율에 맞게 창을 좌,우로 나눠주는 프로그램입니다.

제 스티일로 좌측에는 주로 브라우저, 우측에는 에디터로 되도록 셋팅했습니다.

auto 에 체크하시면 슬라이더가 움직일때 마다 창의 크기가 자동으로 변경됩니다.

이 부분은 원하시는 대로 수정하시면 될거 같습니다.

프로그램에 사용된 ui 파일은 따로 올려두겠습니다.

pygetwindow 모듈이 필요로 하니 pip install pygetwindow 가 필요합니다.

ps) 관리자 권한으로 실행하시기 바랍니다. 또는 pyinstaller 에서 --uac-admin 권한을 주셔도 됩니다.

소스

import os, sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QIcon
from PyQt5 import uic
import pygetwindow as gw
import re
import math

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
design_ui = uic.loadUiType(BASE_DIR + '/design.ui')[0]

class MainWin(QMainWindow, design_ui):

    def __init__(self):
        super().__init__()
        self.initUI()

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

        self.setFixedSize(250,270)

        # move position(right-bottom) main window
        self.screen = app.primaryScreen().size()
        size = self.geometry()
        self.move(self.screen.width() - size.width() - 20, self.screen.height() - size.height() - 90)

        # divide 1/2 screensize
        self.splitWindowSize = {'left' : math.floor(self.screen.width() / 2) ,
                                'right' : math.floor(self.screen.width() / 2)}

        # action
        self.screenSlider.valueChanged.connect(self.sliderSetValue)
        self.btnSplit.clicked.connect(self.split)

        # always on top
        self.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.show()

    def sliderSetValue(self):
        self.currentSliderValue = self.screenSlider.value()
        self.leftPercent.setText(f'{self.currentSliderValue}%')
        self.rightPercent.setText(f'{100-self.currentSliderValue}%')

        self.getSplitWindowSize()

    def getSplitWindowSize(self):
        self.splitWindowSize['left'] = math.floor(self.screen.width() * (self.currentSliderValue / 100))
        self.splitWindowSize['right'] = math.floor(self.screen.width() * ((100-self.currentSliderValue) / 100))

    def moveToWindow(self, winObj, direction='L'):
        if direction == 'L':
            winObj.resizeTo(self.splitWindowSize['left'], self.screen.height()-50)
            winObj.moveTo(0,0)
        elif direction == 'R':
            winObj.resizeTo(self.splitWindowSize['right'], self.screen.height()-50)
            winObj.moveTo(self.splitWindowSize['left'],0)

    def split(self):
        # regular expression ( ex: left : browser , right: editor )
        leftRe = re.compile('.*(chrom|microsoft edge).*', re.I)
        rightRe = re.compile('.*(editplus|visual studio code|pycharm).*', re.I)
        seqNum = 1

        try
            for _, winObj in enumerate(gw.getAllWindows()):
                print(winObj)
                # this program
                if winObj.title == 'Split The Screen':
                    self.myPrg = winObj
                    continue
                if winObj.title == None:
                    continue

                left = leftRe.match(winObj.title)
                right = rightRe.match(winObj.title)

                if left:
                    self.moveToWindow(winObj, direction='L')
                elif right:
                    self.moveToWindow(winObj, direction='R')
                else:
                    if seqNum % 2 == 0:
                        self.moveToWindow(winObj, direction='L')
                    else:
                        self.moveToWindow(winObj, direction='R')

                seqNum+=1

            self.myPrg.activate()
        except Exception as e:
            print(e)

if __name__ == '__main__':

    app = QApplication([])
    app_icon = QIcon()
    app_icon.addFile(BASE_DIR + '/division32.png', QSize(32,32))
    app.setWindowIcon(app_icon)

    mainWindow = MainWin()
    sys.exit(app.exec())

design.ui
0.01MB

반응형