Getting Started with PyQt5 1 - PyQt5 Installation and Hello World Application

study preface

Engage in a visual interface, although it may not be useful, but do it.
insert image description here

Introduction (From Baidu Encyclopedia)

PyQt implements a Python module set. It has more than 300 classes and nearly 6000 functions and methods. It is a multi-platform toolkit that runs on all major operating systems, including UNIX, Windows and Mac.

PyQt adopts dual licenses, and developers can choose GPL and commercial licenses. Before that, the GPL version could only be used on Unix. Starting from version 4 of PyQt, the GPL license can be used on all supported platforms.

Install

PyQt5 can be installed with pip, and other methods are not recommended.
Using pip will automatically select the appropriate PyQt5 version according to your Python version. If you manually download the source code and install it, it is easy to make mistakes, and pip installation is convenient and fast.
Because it is in China, it is faster to use domestic mirroring.

pip3 install PyQt5 -i https://pypi.douban.com/simple

If anaconda has a corresponding virtual environment, you need to activate the virtual environment first and then install it. The same is true under Ubuntu.
insert image description here

Hello World

Let's create a simple interface like this with the title Hello World! Content too.
insert image description here

1. Window Construction

a. Build the basic class

The QWidget class is the base class for all user interface objects.

The window construction needs to be based on the QWidget class. We inherit it as the parent class, rewrite its __init__ method, and call the function of initializing the UI in the __init__ method.

class Example(QWidget):
    def __init__(self):
        QWidget.__init__(self)
    
        #-----------------------------#
        #   界面显示相关内容
        #-----------------------------#
        self.initUI()

    def initUI(self):
        pass

b. Write UI

The previous step did not complete the UI writing. Here we create a simple UI, one is the Hello World on the title, and the other is the Hello World in PyQt5.
In PyQt5, using self.setWindowTitlethe title you can set the UI.
In addition, we can import QTextBrowserto write text, use the .move method to move the text, and use .resize to zoom the window. For details, please refer to the following:

def initUI(self):
    #-----------------------------#
    #   初始化标题,界面大小
    #-----------------------------#
    self.resize(640, 480)
    self.setWindowTitle('Hello World!')

    #-----------------------------#
    #   写一段话
    #   放到10,10
    #   拉伸长度为620,200
    #-----------------------------#
    self.text_browser = QTextBrowser(self)
    self.text_browser.move(10,10)
    self.text_browser.resize(620,200)
    self.text_browser.setText("Hello World again!")

2. The main program runs

This is the Example created above. You don’t need to worry about the details. You just need to know that you can call PyQt5.

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

all codes

import sys

from PyQt5.QtWidgets import (QApplication, QWidget, QTextBrowser)


class Example(QWidget):
    def __init__(self):
        QWidget.__init__(self)
    
        #-----------------------------#
        #   界面显示相关内容
        #-----------------------------#
        self.initUI()

    def initUI(self):
        #-----------------------------#
        #   初始化标题,界面大小
        #-----------------------------#
        self.resize(640, 480)
        self.setWindowTitle('Hello World!')

        #-----------------------------#
        #   写一段话
        #   放到10,10
        #   拉伸长度为620,200
        #-----------------------------#
        self.text_browser = QTextBrowser(self)
        self.text_browser.move(10,10)
        self.text_browser.resize(620,200)
        self.text_browser.setText("Hello World again!")

        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Guess you like

Origin blog.csdn.net/weixin_44791964/article/details/130545018