[PYTHON] Introduction to PyQt

Qt 5.14 Release!! I'm sorry for the story that has nothing to do with the theme suddenly. Everyone, Qt 5.14 has been released! !! スクリーンショット 2019-12-14 22.02.19.png When I tried to install Qt for the first time in a while, I noticed (actually, I noticed when I was with hermit ...). I thought about installing 5.14 immediately, but this time the purpose was not to use new features, but considering compatibility with binding tools etc., I thought that the latest LTS 5.12.6 was safe. When I have time, I'll try 5.14 as well.

Introduction

This is my 7th post, HW developer ynuma. I will briefly write about the latest situation.

It's early, and my daughter is almost 3 years old. The other day, I took a picture of Shichigosan. Holidays are a daughter-centered life.

There is no big change in the work situation, but I use Python to make evaluations that incorporate machine learning. I rarely use Qt, but I am motivated to create applications as a hobby, so I would like to continue learning.

About this theme

This is the main subject, but this time the theme is about PyQt. Do you all know?

Initially, I was planning to focus on work-related machine learning, but unfortunately I couldn't do it this time because I found out that Qt itself does not have a function to support machine learning (if I made a mistake, I couldn't do it. Please teach). If you're doing machine learning, I think the trend is to use Python. Since Python has a relatively long history, I think that there are abundant libraries such as graphing, but I thought that it would be convenient if I could use the functions of Qt that I have some knowledge from Python.

Reference book

By Kazuo Asano "Let's make a GUI with Python" I will refer to it for a considerable part. 12/12 Article posted by spiralgear Introduction of Qt-related books that could be supplemented within the range of personal observation But it is introduced. There are plenty of diagrams and explanations, and even beginners can easily understand it, so I recommend it! !! I think it's also attractive to have a lot of examples of QtQuick.

Recommended for people like this

First of all, since it is an introductory edition, I am targeting those who know Qt to some extent but have never used PyQt. Or if you're using Python and want to take advantage of Qt's rich features, you'll find it helpful.

What is PyQt?

Qt's Python binding was developed by Riverbank Computing in the United Kingdom. Binding means connecting. In short, it's what connects Python and Qt. More specifically, I think it's meant to allow you to call Qt from Python.

History I didn't know exactly when it was developed, but it seems to have existed around 2007. PySide, which was developed by Nokia (now The Qt Company), is said to be of the same type, but it seems that it was developed as an alternative due to a failure to reach a license agreement with PyQt's Riverbank Computing.

license

PyQt is the GPL. For reference, PySide is the LGPL. If you prioritize corporate profits, you may want to use PySide, but PyQt seems to be more convenient. Which one to adopt as a company is a difficult place.

Install Below is a brief summary of the steps to get PyQt working.

  1. Anaconda Install (Distribution for Python language)
  2. Qt Install
  3. Build a virtual environment for PyQt from Anaconda
  4. Install PyQt in the created virtual environment

Anaconda Install (Distribution for Python language)

スクリーンショット 2019-12-14 18.35.05.png Anaconda seems to provide the following functions. ・ Multiple Python virtual environments can be created ・ Multiple Python versions can be used properly -It is possible to use the Python interpreter properly for each environment. ・ Package management system conda can be used

There is a lot of information on the Web about the procedure details, so I will omit it.

Qt Install As you all know, it's easy to download and run the Qt online Installer from the site below. https://www.qt.io/download-open-source

Build a virtual environment for PyQt from Anaconda

  1. Start Anaconda navigator. スクリーンショット 2019-12-14 18.35.40.png
  2. Run Environment → Create to create a virtual environment for PyQt. Please refer to the following for the name and Packages. スクリーンショット 2019-12-14 18.36.37.png

Install PyQt in the created virtual environment

  1. Click the triangle button next to the name of the disguise environment on Anaconda Navigator and select Open Terminal. スクリーンショット 2019-12-14 23.18.57.png
  2. Install PyQt5 from the terminal. I think the following example will be helpful.
Last login: Fri Dec 13 03:13:56 on ttys000
(base) numac:~ ynumajir$ /Users/ynumajir/.anaconda/navigator/a.tool ; exit;
(Python3_7) bash-3.2$ pip install PyQt5
Collecting PyQt5
  Downloading https://files.pythonhosted.org/packages/93/5b/2ba062584e8b407b443b7ac7b6687e157d22b0f875cfd1dbe5baf82f6177/PyQt5-5.13.2-5.13.2-cp35.cp36.cp37.cp38-abi3-macosx_10_6_intel.whl (39.8MB)
     |████████████████████████████████| 39.8MB 23.1MB/s 
Collecting PyQt5_sip<13,>=4.19.19
  Downloading https://files.pythonhosted.org/packages/ca/77/ac0e8908be37523f0d93b8002641d11cfbc231c3e9dbaa4835f8073b397d/PyQt5_sip-12.7.0-cp37-cp37m-macosx_10_9_x86_64.whl (62kB)
     |████████████████████████████████| 71kB 8.5MB/s 
Installing collected packages: PyQt5-sip, PyQt5
Successfully installed PyQt5-5.13.2 PyQt5-sip-12.7.0
(Python3_7) bash-3.2$ pip list
Package    Version            
---------- -------------------
certifi    2019.11.28         
pip        19.3.1             
PyQt5      5.13.2             
PyQt5-sip  12.7.0             
setuptools 42.0.2.post20191201
wheel      0.33.6             
(Python3_7) bash-3.2$ 

Run Sample program

Below is the python code that displays a simple MainWindow.

QMainWindow.py



# - * - coding: utf8 - * -
  
import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt test(QMainWindow)'
        self.width = 400
        self.height = 200
        self.setWindowTitle(self.title)
        self.setGeometry(0, 0, self.width, self.height)
        label = QLabel('This is PyQt test.', self)
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

Below are the execution results. $ python QMainWindow.py スクリーンショット 2019-12-15 0.37.27.png Now you can use the Qt library from Python.

At the end

This time, I've summarized the steps up to the point where you can make a simple call to Qt using PyQt. Since the Anaconda and python environments were originally set up on the PC used, it was relatively easy to start up. As a result, I think it has become possible to display machine learning results on the Qt GUI. Next time, I would like to find out if I can make a 3D graph with PyQt as easily as possible.

Recommended Posts

Introduction to PyQt
Introduction to PyQt4 Part 1
Introduction to MQTT (Introduction)
Introduction to Scrapy (1)
Introduction to Scrapy (3)
Introduction to Supervisor
Introduction to Scrapy (2)
[Linux] Introduction to Linux
Introduction to Scrapy (4)
Introduction to discord.py (2)
Introduction to discord.py
Introduction to Lightning pytorch
Introduction to Web Scraping
Introduction to EV3 / MicroPython
Introduction to Python language
Introduction to TensorFlow-Image Recognition
Introduction to OpenCV (python)-(2)
Introduction to Dependency Injection
Introduction to Private Chainer
Introduction to machine learning
AOJ Introduction to Programming Topic # 1, Topic # 2, Topic # 3, Topic # 4
Introduction to electronic paper modules
A quick introduction to pytest-mock
Introduction to dictionary lookup algorithm
Introduction to Monte Carlo Method
[Learning memorandum] Introduction to vim
Introduction to PyTorch (1) Automatic differentiation
opencv-python Introduction to image processing
Introduction to Python Django (2) Win
Introduction to Cython Writing [Notes]
An introduction to private TensorFlow
Kubernetes Scheduler Introduction to Homebrew
An introduction to machine learning
[Introduction to cx_Oracle] Overview of cx_Oracle
A super introduction to Linux
Introduction
AOJ Introduction to Programming Topic # 7, Topic # 8
[Introduction to pytorch-lightning] First Lit ♬
Introduction to Anomaly Detection 1 Basics
Introduction to RDB with sqlalchemy Ⅰ
[Introduction to Systre] Fibonacci Retracement ♬
Introduction to Nonlinear Optimization (I)
Introduction to serial communication [Python]
AOJ Introduction to Programming Topic # 5, Topic # 6
Introduction to Deep Learning ~ Learning Rules ~
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
[Introduction to cx_Oracle] (8th) cx_Oracle 8.0 release
Introduction to discord.py (3) Using voice
An introduction to Bayesian optimization
Deep Reinforcement Learning 1 Introduction to Reinforcement Learning
Super introduction to machine learning
Introduction to Ansible Part ③'Inventory'
Series: Introduction to cx_Oracle Contents
[Introduction] How to use open3d
Introduction to Python For, While
Introduction to Deep Learning ~ Backpropagation ~
Introduction to Ansible Part ④'Variable'
Introduction to vi command (memorandum)
Introduction to Linux Commands ~ LS-DYNA Edition ~