In this Pyside2 GUI article i want to show you How To Make Center The Window, basically there is no widget for this work, but manually we are using frameGeometry() and QDesktopWidget() for this action.
Check Python GUI Development With Pyside2
1: Getting Started With Pyside2 | Qt For Python
2: Pyside2 GUI Creating First Window
3: Pyside2 GUI Creating Window Icon
4: Pyside2 GUI How To Create Icon Modes
5: Pyside2 GUI How To Create Tooltip
6: Pyside2 GUI QPushButton With Signal And Slot
So now this is the complete code for Pyside2 GUI How To Make Center The Window
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
from PySide2.QtWidgets import QApplication, QWidget, QDesktopWidget import sys from PySide2.QtGui import QIcon class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Center Window") self.setGeometry(500,400,500,400) self.setIcon() self.center() def setIcon(self): appIcon = QIcon("icon.png") self.setWindowIcon(appIcon) def center(self): qRect = self.frameGeometry() centerPoint = QDesktopWidget().availableGeometry().center() qRect.moveCenter(centerPoint) self.move(qRect.topLeft()) myapp = QApplication(sys.argv) window = Window() window.show() myapp.exec_() sys.exit() |
OK in the above code first we have imported our classes from Pyside2 library
1 2 3 |
from PySide2.QtWidgets import QApplication, QWidget, QDesktopWidget import sys from PySide2.QtGui import QIcon |
And this is our main window class that inherits from QWidget, in that class we are going to add the window title , window geometry and minimum width and height for the window.
also you can check Python Object Oriented Programming Articles in this link. Python Object Oreinted Programming. also we have added the setIcon() and center() methods in this class.
1 2 3 4 5 6 7 8 9 |
class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Center Window") self.setGeometry(500,400,500,400) self.setIcon() self.center() |
Also this method is for setting our window icon
1 2 3 |
def setIcon(self): appIcon = QIcon("icon.png") self.setWindowIcon(appIcon) |
So now this is the method that we are going to make center our window.
1 2 3 4 5 |
def center(self): qRect = self.frameGeometry() centerPoint = QDesktopWidget().availableGeometry().center() qRect.moveCenter(centerPoint) self.move(qRect.topLeft()) |
in the above code the frameGeometry() is the geometry of the widget relative to its parent including any window frame. By default, this property contains a value that depends on the user’s platform and screen geometry.
What is QDesktopWidget Class ?
Systems with more than one graphics card and monitor can manage the physical screen space available either as multiple desktops, or as a large virtual desktop. This class provides information about the user’s desktop, such as its total size, number of screens, the geometry of each screen, and whether they are configured as separate desktops or a single virtual desktop.Widgets provided by Qt use this class to place tooltips, menus and dialog boxes on the correct screen for their parent or application widgets. Applications can use this class to obtain information that can be used to save window positions, or to place child widgets and dialogs on one particular screen.
So in here every Pyside2 application must create an application object. The sys.argv
parameter is a list of arguments from a command line.
Python scripts can be run from the shell. It is a way how we can control the startup of our scripts.
1 |
myapp = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. The mainloop ends if we call the exit()
method or the main widget is destroyed. The sys.exit()
method ensures a clean exit. The environment will be informed how the application ended.
The exec_()
method has an underscore. It is because the exec
is a Python keyword. And thus, exec_()
was used instead.
1 2 |
myapp.exec_() sys.exit() |
So now run the complete code and this will be the result
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email