Maximize and restore the window

0

I have the following code:

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


class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("23.ui",self)
        self.s = 0

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        self.bmax.clicked.connect(self.W_Size)
        self.bmin.clicked.connect(self.showMinimized)
        self.bclose.clicked.connect(self.close)



    def mousePressEvent(self,event):
        if event.button() == QtCore.Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
            event.accept()

    def mouseMoveEvent(self,event):
        if event.buttons() == QtCore.Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)
            event.accept()

    def W_Size(self):

        if self.s == 0:
            self.showMaximized()
            self.s=1

        elif self.s==1:
            self.s = 0
            self.resize(878,575)    
            qRect = self.frameGeometry()
            centerPoint  = QDesktopWidget().availableGeometry().center()
            qRect.moveCenter(centerPoint)
            self.move(qRect.topLeft())


app = QApplication([])
p = Principal()
p.show()
app.exec_()

In which the variable s is responsible for manipulating the action taken by the self.bmax button event.

The idea is that by pressing the self.bmax button, compare if the value of the variable = 0 then when you press the button the window will be displayed in full screen and the value of s will change now "s = 1"; so that when you press the button again, change the size of the window and position it in the center. Again s return the value of "0", to repeat the whole process,

but it is in this last part where the code no longer works. Since it does not perform the action again to show the window in full screen.

    
asked by Revsky01 14.07.2018 в 06:17
source

1 answer

1

The problem is not the variable s , but when you maximize the window it changes state, the solution is to restore it to its normal state with showNormal() as shown below:

def W_Size(self):
    if self.s == 0:
        self.showMaximized()
        self.s=1
    elif self.s==1:
        self.s = 0
        self.showNormal()
        self.resize(878, 575) 
        qRect = self.frameGeometry()
        centerPoint  = QDesktopWidget().availableGeometry().center()
        qRect.moveCenter(centerPoint)
        self.move(qRect.topLeft())

Although a more elegant way would be to avoid creating that flag and use the isMaximized() method.

def W_Size(self):
    if self.isMaximized():
        self.showNormal()
        self.resize(878, 575) 
        qRect = self.frameGeometry()
        centerPoint  = QDesktopWidget().availableGeometry().center()
        qRect.moveCenter(centerPoint)
        self.move(qRect.topLeft())
    else:
        self.showMaximized()
    
answered by 14.07.2018 / 06:32
source