This article mainly introduces PYQT5 every day must learn to create a window center effect, so that the application window display in the center of the screen, with a certain reference value, interested in small partners can refer to
The example of this article for you to share how PYQT5 can create a desktop screen on the center window of the specific code for everyone to refer to, the specific content is as follows
The following script shows how we can create a window centered on the desktop screen.
#!/usr/bin/python3#-*-coding:utf-8-*-"" "PYQT5 Tutorial this program is to display a window in the center of the screen. Author: My World You've been to the blog: Http://blog.csdn.net/weiaitaowang last edited: July 30, 2016 "" "Import sysfrom pyqt5.qtwidgets Import Qapplication, Qwidget, Qdesktopwidgetclass Example (qwidget): def __init__ (self): super (). __init__ ( ) Self.initui () def initui (self): self.setgeometry () self.center () Self.setwindowtitle ( ' Window center ') Self.show () def Center (self): QR = self.framegeometry () CP = Qdesktopwidget (). Availablegeometry ( ). Center () Qr.movecenter (CP) Self.move (Qr.topleft ()) If __name__ = = ' __main__ ': app = Qapplication (SYS.ARGV) ex = Example () sys.exit (App.exec_ ())
The Qtgui.qdesktopwidget class provides information about the user's desktop, including the screen size.
Self.center ()
Place the code for the Center window in the Custom Center () method.
qr = Self.framegeometry ()
The Framegeometry () method allows us to create an invisible rectangle and set its own width and height based on the width height of the main window. The simple idea is to assign the geometry of the control (Qwidget) to the QR
CP = Qdesktopwidget (). Availablegeometry (). Center ()
Calculate the screen resolution of your monitor. Based on the obtained resolution we get the center point of the screen.
Qr.movecenter (CP)
Our rectangle (QR) already has width and height, now set the center of the move rectangle (movecenter) to the center point (CP) of the screen, the size of the rectangle is constant.
Self.move (Qr.topleft ())
Move the upper-left corner of the application window to the upper-left corner of the qr rectangle, allowing the application window to appear in the center of the screen.
After the program executes