PyQt5 basic window control ----- QWidget

Keywords: Windows Qt

Pyqt5 learning path, click to have pyqt5 learning notes

QWidget class is the base class of all user interface objects. All windows and spaces are directly or indirectly inherited from QWidget

The window control (Widget) is the main element to build the interface in PyQt.

Window coordinate system

PyQt uses a unified coordinate system to locate the position and size of the window control. It takes the origin of the upper left corner of the screen as the positive x-axis direction to the right and the positive y-axis direction to the down.
There is also its own coordinate system inside the window. The origin of the upper left corner is the positive X-axis direction to the right and the positive y-axis direction to the down. The area around the x-axis and y-axis is called the customer area. The customer area is surrounded by the title block and frame

The member functions of QWidget can be divided into three types

  • QWidget directly provides member functions: x(), y() get the coordinates of the upper left corner of the window, width(), height() get the width and height of the client area;
  • The member functions provided by QWidget geometry(): x(), y() get the coordinates of the upper left corner of the client area, and width(), height() get the width and height of the client area;
  • The member functions provided by QWidget's frameGeometry(): x(), y() get the coordinates of the upper left corner of the client area, width(), height() get the width and height of the whole window including the client area, title bar and border;
Common geometry
  • Geometry without borders
  • Geometry with various borders on the outside
  1. QWidget common functions without borders
    In general, the part without border is called client area, which is a rectangle. In Qt, QRect class is used to save the rectangle, and the following functions are used to change its size and position.
  • Change customer area
QWidget.resize(width, height)
QWidget.resize(QSize)
  • Get client area size
QWidget.size()
  • Get the height and width of the customer area
QWidget.width()
QWidget.height()
  • Set the width and height of the client area
QWidget.setFixedWidth(int width)  # Fixed height, only width can be changed
QWidget.setFixedHeight(int height)  # Fixed width, only height can be changed
QWidget.setFixedSize(QSize size)  # Fixed size, cannot be changed by mouse
QWidget.setFixedSize(int width, int height)  # Fixed size, cannot be changed by mouse
QWidget.setGeometry(int x, int y, int width, int height)
QWidget.setGeometry(QRect rect)
  1. Common functions of QWidget containing border
  • Get the size and location of the window
QWidget.frameGeometry()
  • Set window position
QWidget.move(int x, int y)
QWidget.move(QPoint point)
  • Get the coordinates of the upper left corner of the window
QWidget.pos()
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QDesktopWidget, QHBoxLayout, QWidget, QPushButton

def onClick_Button():
    print('1,QWidget,window')
    print("widget.x()= %d" % widget.x())    # Window abscissa
    print("widget.y()= %d" % widget.y())    # Window ordinate
    print("widget.width()= %d" % widget.width())    # Workspace width
    print("widget.height()= %d" % widget.height())    # Height of working area

    print('------------------------------------------')

    print('2,gemetry,work area')
    print("widget.geometry().x()= %d" % widget.geometry().x())    # Work area abscissa
    print("widget.geometry().y()= %d" % widget.geometry().y())    # Work area ordinate
    print("widget.geometry().width()= %d" % widget.geometry().width())    # Workspace width
    print("widget.geometry().height()= %d" % widget.geometry().height())    # Workspace width

    print('------------------------------------------')

    print('3,frameGeometry,work area+Title Bar')
    print("widget.frameGeometry().x()= %d" % widget.frameGeometry().x())    # Window abscissa
    print("widget.frameGeometry().y()= %d" % widget.frameGeometry().y())    # Window ordinate
    print("widget.frameGeometry().width()= %d" % widget.frameGeometry().width())    # Window width
    print("widget.frameGeometry().height()= %d" % widget.frameGeometry().height())    # Window height

app = QApplication(sys.argv)

widget = QWidget()
btn = QPushButton(widget)
btn.setText("Button")
btn.clicked.connect(onClick_Button)

btn.move(24, 52)

widget.resize(300, 244)
widget.move(250, 200)

widget.setWindowTitle('QWidget')

widget.show()

sys.exit(app.exec_())



Add Icon
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QDesktopWidget, QHBoxLayout, QWidget, QPushButton
from PyQt5.QtGui import QIcon

class IconForm(QMainWindow):
    def __init__(self, parent=None):
        super(IconForm, self).__init__(parent)
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 250, 250)
        # Set the title of the main window
        self.setWindowTitle("Main window application")
        # Set window size
        self.resize(400, 300)
        # Set window icon
        self.setWindowIcon(QIcon('./about_us_bk.jpg'))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = IconForm()
    main.show()
    sys.exit(app.exec_())
Add prompt
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QDesktopWidget, QHBoxLayout, QWidget, QPushButton, QToolTip
from PyQt5.QtGui import QFont

class TooltipForm(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.btn()
    def initUI(self):
        QToolTip.setFont(QFont('SansSerif', 12))
        self.setToolTip('Today is<b>Friday</b>')
        self.setGeometry(300, 300, 200, 300)
        self.setWindowTitle('Set control prompt information')
    def btn(self):
        self.button1 = QPushButton('My button')
        self.button1.setToolTip('This is a button')

        layout = QHBoxLayout()
        layout.addWidget(self.button1)

        mainFrame = QWidget()
        mainFrame.setLayout(layout)
        self.setCentralWidget(mainFrame)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = TooltipForm()
    main.show()
    sys.exit(app.exec_())
Published 21 original articles, won praise 27, visited 832
Private letter follow

Posted by ElArZ on Sat, 22 Feb 2020 01:58:24 -0800