2016-06-09 5 views
2

이상 코드는 이상하게 문제가 있습니다. 이유를 모르겠지만, 의 창 크기를 923 이상으로 설정하면 창이 화면 중간에 배치되지 않습니다. 화면 왼쪽 상단으로 이동합니다.PyQt5 이상한 창 위치 문제

from PyQt5.QtWidgets import QApplication,QDesktopWidget,QMainWindow 
from PyQt5 import QtCore 

import sys 

class cssden(QMainWindow): 
    def __init__(self): 
     super().__init__() 


     self.mwidget = QMainWindow(self) 
     self.setWindowFlags(QtCore.Qt.FramelessWindowHint) 


     #size 
     self.setFixedSize(1100,923) # <--- set this 924 and more 
     self.center # <-- function that set the window middle of the screen 
     self.show() 

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

app = QApplication(sys.argv) 
app.setStyleSheet("QMainWindow{background-color: rgb(30,30,30);border: 1px solid black}") 

ex = cssden() 
sys.exit(app.exec_()) 

왜? 이것은 1920x1080 인 내 화면 해상도에 관한 것입니까? 왜 숫자가 923 인 지 이해할 수 없으며이를 수정하는 방법을 알고 있습니다.

편집 : 923 화면의 왼쪽 상단 구석이 왜 PyQt 중심으로 바뀝니 까? 각 해상도마다 기본 숫자가 있습니까?

+0

나는 실제로'center()'를 호출하지 않는다는 것을 알아 차렸다. 그것이 문제가 될 수 있습니다. –

+0

@BrendanAbel 아니요, '923'아래의 창은 화면 중앙에 있습니다. 저것을보십시오'self.center' – GLHF

+0

@ GLHF @BrendanAbel 맞습니다. 윈도우가 중앙에 위치하는 동안 실제로 위 코드에서 self.center()를 호출하지 않습니다. 당신은 단순히 참조를 얻고 있습니다. 'self.center()'에 괄호가 필요합니다. :-) – jszakmeister

답변

0

이 대답에서이 솔루션을 시도해보십시오 PyQt4 center window on active screen

def center(self): 
    frameGm = self.frameGeometry() 
    screen = QtGui.QApplication.desktop().screenNumber(QtGui.QApplication.desktop().cursor().pos()) 
    centerPoint = QtGui.QApplication.desktop().screenGeometry(screen).center() 
    frameGm.moveCenter(centerPoint) 
    self.move(frameGm.topLeft()) 

이 기능은 마우스 포인트의 위치를 ​​기반으로합니다. screenNumber 함수를 사용하여 현재 활성화되어있는 화면을 확인합니다. 그런 다음 해당 모니터의 screenGeometry 인 과 해당 화면의 중심점을 찾습니다. 모니터 해상도가 다른 경우에도이 방법을 사용하면 은 화면 가운데에 창을 배치 할 수 있어야합니다.