[PYTHON] GUI creation with Pyside2 <Kindergarten level>

Introduction

Since I was studying Pyside2, I somehow understood QApplication and QWindow, which are the first to get caught, so I summarized them.

What to make this time

Completion drawing

It's a very simple window. image.png

code

import sys
from PySide2.QtWidgets import *

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = QWidget()
    main_window.resize(600,200)
    main_window.show()
    sys.exit(app.exec_()) 

Commentary

from PySide2.QtWidgets import *

I'm importing QtWidgets. The QtWidgets contains a class that creates the QWidget, which is the base of the Window, and the QPushbutton button.

from PySide2.QtWidgets import QApplication,QWidget

You can also impot by specifying a class as above without using * (wildcard).

import sys

Since sys.argv () is used, the sys module is also imported. We will discuss sys.argv () later.

if __name__ == '__main__':

ʻIf name =='main` is omitted because there are many explanations on the net.

    app = QApplication(sys.argv)

Store QApplication in the variable ʻapp. QApplication` is like the basis for putting a GUI on.

    main_window = QWidget()

The QWidget is stored in the variable main_window. QWidget is the window where you place the buttons and labels. The relationship between QApplication and QWindow is like this. image.png

    main_window.resize(600,200)

Specify the Window size. If you change the value, the size will change. It seems that you can also change the color and shape.

    main_window.show

Display the created main_window =QWindow ().

    sys.exit(app.exec_()) 

ʻApp.exec_ ()QApplication.exec_ ()creates a loop that keeps the Window displayed. If you do not loop, the displayed Window will disappear as soon as the program ends. sys.exit_ () is a command to terminate the program. Waiting for the end of ʻapp.exec_ (). ʻWhen the app.exec_ ()ends, it receives0and terminates the program itself. If you do not writesys.exit_ (), the following program will be executed after exiting ʻapp.exec_ ().

Postscript

If you study Pyside, searching with PyQt has more information than Pyside. You can refer to this page for the difference between PyQt and Pyside. https://qiita.com/nullpo24/items/c70e02c26ef5cade9480

Recommended Posts

GUI creation with Pyside2 <Kindergarten level>
GUI creation with Pyside Part 2 <Use of class>
Simple sales tool creation with Python GUI: Quote creation
Simple sales tool creation with Python GUI: Employee number search
Simple sales tool creation with Python GUI: Cost rate calculation
Simple sales tool creation with Python GUI: Desktop application released
Try GUI programming with Hy
Creating GUI tools with pyinstaller
Web application creation with Django
[GUI with Python] PyQt5-Layout management-
[GUI with Python] PyQt5 -Preparation-
Cascade classifier creation with opencv
[GUI with Python] PyQt5 -Paint-