PySide Center Screen and Window State Maximized in PySide

As you know PySide provide stylish GUI comparing Tkinter and feature rich QT base application. So first, here we learn how to set Center Screen window in Pyside in Python? And also we learn Window State Maximize in Pyside in Python. Here we also include how to Set window title in PySide?

Let’s find it in following points:

  1. Set window Size in PySide6 and Set Window Location in PySide6
  2. Python Center Screen Window in PySide6
  3. Set Window State Maximized in PySide6
  4. Set Window Title in PySide6


Window State Maximize in PySide


How to Set Center Screen in Tkinter and Set window size in Tkinter?

(1). Set Window Size in PySide6 and Set Window Location in Pyside6 :

Suppose, you want to create a Form using PySide and want to display at run time with specific size with specific location on screen. Then here PySide provide ‘setGeometry’ or ‘move’ and ‘resize’ function. The syntax is used in following way:


‘setGeometry’ Syntax:
MainForm.setGeometry(Form_Left, Form_Top, Form_Width, Form_Height)

Example:
from PySide6.QtWidgets import QMainWindow, QApplication

SKapp = QApplication()
MyPyForm = QMainWindow()

#Set Form's Location and Size
MyPyForm.setGeometry(150, 100, 330, 250)

MyPyForm.show()

SKapp.exec()




‘move’ Syntax:
MainForm.move(Left_Location, Top_Location)


‘resize’ Syntax:
MainForm.resize(Form_Width, Form_Height)


Now, we are using both function in given example:
from PySide6.QtWidgets import QMainWindow, QApplication

SKapp = QApplication()
MyPyForm = QMainWindow()

#Set Location of Form in PySide
MyPyForm.move(150, 100)
#Set Form Size in PySide
MyPyForm.resize(330, 250)

MyPyForm.show()

SKapp.exec()


Now, you will get same output when run application like bellow:

Set Window Size and Location in Pyside



(2). Python Center Screen Window in PySide6 :

In this point, we learn how to set start position for form in center screen. For center screen form in Pyside we have to import following:
from PySide6.QtGui import QScreen

Here are following two ways from which you can set Center Screen in Python.

Note: Remember one thing before positioning form in center screen, you have to first define the form or dialog size then write center screen code.

First we use ‘QScreen.availableGeometry(QApplication.primaryScreen())’. This will show screen available geometry.

PySide Center Screen Example 1:
from PySide6.QtWidgets import QMainWindow, QApplication
from PySide6.QtGui import QScreen

SKapp = QApplication()
MyPyForm = QMainWindow()

#Set Form Size
MyPyForm.resize(330, 250)

#Get Screen geometry
SrcSize = QScreen.availableGeometry(QApplication.primaryScreen())
#Set X Position Center
frmX = (SrcSize.width() - MyPyForm.width())/2
#Set Y Position Center
frmY = (SrcSize.height() - MyPyForm.height())/2
#Set Form's Center Location
MyPyForm.move(frmX, frmY)

MyPyForm.show()

SKapp.exec()


Pass Value from Text To Label in Tkinter.


Pyside Center Screen Example 2:
from PySide6.QtWidgets import QMainWindow, QApplication
from PySide6.QtGui import QScreen

SKapp = QApplication()
MyPyForm = QMainWindow()

#Set Form's Location and Size
MyPyForm.resize(330, 250)

#Get Screen Center Position
Fmcenter = QScreen.availableGeometry(QApplication.primaryScreen()).center()
Fmscr = MyPyForm.frameGeometry()

#Set Form in Center Position
Fmscr.moveCenter(Fmcenter)

MyPyForm.show()

SKapp.exec()



Both codes will show same output:
Pyside Center Screen Window





(3). Set Window State Maximized in Pyside6 :

Here SKOTechLearn describe two way to set window state maximized in PySide, and both will show same output as described bellow:

PySide6 Maximized Form Example 1:

from PySide6.QtWidgets import QMainWindow, QApplication
from PySide6.QtGui import QScreen

SKapp = QApplication()
MyPyForm = QMainWindow()

#Set Form's Location and Size
MyPyForm.resize(330, 250)

SrcSize = QScreen.availableGeometry(QApplication.primaryScreen())

#Get Screen Width
frmX=SrcSize.width()

#Get Screen Height
frmY=SrcSize.height()

#Pyside Window State maximized
MyPyForm.setGeometry(0, 0, frmX, frmY)

MyPyForm.show()

SKapp.exec()



PySide6 Maximize Form Example 2:

import sys
from PySide6.QtWidgets import QMainWindow, QApplication
from PySide6.QtGui import QScreen

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'PySide6 Maximize Window State'
        
        #Pyside6 Maximize Form
        MyScn = QScreen
        self.setGeometry(0, 0, MyScn.availableGeometry(QApplication.primaryScreen()).width(), MyScn.availableGeometry(QApplication.primaryScreen()).height())  
    
        self.show()    
    
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec())


You will get maximized form:
PySide Maximize Window State




(4). Set Window Title in PySide6 :

The simple way to set window title or form title in PySide using ‘setWindowTitle’. Her we use it with two way at design time and change Form title in PySide at run time.


(i). Set Window Title at design time :
import sys
from PySide6.QtWidgets import QApplication, QMainWindow

app = QApplication(sys.argv)
mainwindow = QMainWindow()

mainwindow.setGeometry(300, 300, 300, 150)

#Set Window Title in PySide
mainwindow.setWindowTitle('PySide Set Window Title')

mainwindow.show()

app.exec()   



(ii). Set Window Title at run time :

Here we use ‘QPushButton’ to change Title at run time when we click on button.
import sys
from PySide6.QtWidgets import QMainWindow, QPushButton, QApplication

class MyTitle(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
    def initUI(self):      

        Pysbtn = QPushButton('Show title', self)
        Pysbtn.move(70, 40)
        
        #Call Click Event which is defined bellow
        Pysbtn.clicked.connect(self.ClickToChangeTitle)
        
        self.setGeometry(300, 300, 300, 150)
        self.show()


    #QPushButton Click Event in PySide    
    def ClickToChangeTitle(self):
            self.setWindowTitle('Click to Change Title in Pyside')
        
if __name__ == '__main__':  
    app = QApplication(sys.argv)
    ex = MyTitle()
    sys.exit(app.exec())


Output:
Set Window Title in PySide


Set Window Title in Python


Now here are some Pyside windows tips, this way you can easily Maximized Window in PySide and Also Set Center Screen Window in PySide in Python. And Also Set Window Title or Form Title in PySide.

0 comments: