Example of how PyQt programms to display a form in the center of the screen

  • 2021-06-28 13:30:41
  • OfStack

Learn PyQt programming these days, use CSDN to record 1 thing you've learned, so you won't forget to laugh later


import sys 
from PyQt4 import QtGui # Import Control Module 

class Center(QtGui.QWidget): # Inherit parent class QWidget
  def __init__(self,parent=None):
    QtGui.QWidget.__init__(self,parent) # Two constructors are required, 1 individual Center Class, 1 individual QWidget Class 

    self.setWindowTitle('center')
    self.resize(600,300) # Initialize Form Size 
    self.center() 

  def center(self): # Implement the form in the center of the screen 
    screen =  QtGui.QDesktopWidget().screenGeometry() #QDesktopWidget by 1 Class, call screenGeometry Function to get screen size 
    size  =  self.geometry() # Ditto 
    self.move((screen.width()-size.width())/2,(screen.height()-size.height())/2) # call move Move to specified location 

app=QtGui.QApplication(sys.argv)
qb=Center()
qb.show()
sys.exit(app.exec_())

Related articles: