PYQT5 every day must learn to create a window center effect

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.