I played with PyQt5 and Python3

What is Qt?

Qt (Cute) is an application user interface (UI) framework written in the C ++ language.

Qt is widely known as a GUI toolkit, but it is also widely used in non-GUI programs such as console tools and servers. From Wikipedia

A framework that makes it easy to create GUI applications. It also supports cross-platform development.

What is PyQt?

PyQt is a cross-platform GUI toolkit, Qt's Python binding, and is one of the options for GUI programming in Python. From Wikipedia

You can easily create Qt and GUI applications set for Python.

Benefits of PyQt

Problems with PyQt

First, prepare an environment where you can use PyQt5

procedure

  1. Install Python
  2. Install Qt (may take a long time)
  3. Install PyQt

I won't explain the environment settings here, so ggrks

Let's actually write the code

First, let's open the window

window.py


#!/usr/bin/env python3

import sys
from PyQt5.QtWidgets import *

class MainWindow(QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()

    main_window.show()
    sys.exit(app.exec_())

When I run it, an empty window appears

Let's add a button and a title

button.py


#!/usr/bin/env python3

import sys
from PyQt5.QtWidgets import *

class MainWindow(QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        button = QPushButton('button')

        layout = QGridLayout()
        layout.addWidget(button)
        self.setLayout(layout)
        self.setWindowTitle("Button")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()

    main_window.show()
    sys.exit(app.exec_())

With button and title

Let's read and display the value of the text field

input-output.py


#!/usr/bin/env python3

import sys
from PyQt5.QtWidgets import *

class MainWindow(QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.button = QPushButton('Read / display')
        self.button.clicked.connect(self.output)
        self.inputText = QLineEdit()
        self.inputText.setText("")

        self.outputText = QLineEdit()
        self.outputText.setText("")
        self.outputText.setReadOnly(True)

        textLayout = QHBoxLayout()
        textLayout.addWidget(self.inputText)
        textLayout.addWidget(self.outputText)

        layout = QVBoxLayout()
        layout.addLayout(textLayout)
        layout.addWidget(self.button)
        self.setLayout(layout)
        self.setWindowTitle("Button")

    def output(self):
        self.outputText.setText(self.inputText.text())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()

    main_window.show()
    sys.exit(app.exec_())

When you enter text and press the button, the text is output

Display the character string entered in QGraphicsView

string-graphic.py


#!/usr/bin/env python3

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class ShowString(QGraphicsItem):
    def __init__(self, width=200, height=10, text=""):
        super(ShowString, self).__init__()
        self.width = width
        self.height = height
        self.text = text

    def paint(self, painter, option, widget):
        painter.setPen(Qt.black)
        painter.drawText(0, 20, self.text)

    def boundingRect(self):
        return QRectF(0,0,400,100)

    def setText(self, text):
        self.text = text
        self.update()

class MainWindow(QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.graphicView = QGraphicsView()
        self.showString = ShowString()
        scene = QGraphicsScene(self.graphicView)
        scene.setSceneRect(0, 0, 400, 100)
        self.graphicView.setScene(scene)
        self.graphicView.resize(300,50)
        scene.addItem(self.showString)

        self.button = QPushButton('Read / display')
        self.button.clicked.connect(self.output)
        self.inputText = QLineEdit()
        self.inputText.setText("")

        self.outputText = QLineEdit()
        self.outputText.setText("")
        self.outputText.setReadOnly(True)

        textLayout = QHBoxLayout()
        textLayout.addWidget(self.inputText)
        textLayout.addWidget(self.outputText)

        layout = QVBoxLayout()
        layout.addWidget(self.graphicView)
        layout.addLayout(textLayout)
        layout.addWidget(self.button)
        self.setLayout(layout)
        self.setWindowTitle("Button")

    def output(self):
        self.outputText.setText(self.inputText.text())
        self.showString.setText(self.inputText.text())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()

    main_window.show()
    sys.exit(app.exec_())

Added character string output in QGraphicsView to the previous program

I may add a sample program again. .. ..

Continued: Try to open a subwindow with PyQt5 and Python

Impressions / Discussion

That's all for playing with PyQt5 and Python3. I like it because I can design GUIs really intuitively.

reference

I used it as a reference below. Thank you very much. http://d.hatena.ne.jp/mFumi/20141112/1415806010 http://qiita.com/kenasman/items/55505654823e9d040e6e

Recommended Posts

I played with PyQt5 and Python3
[Python] I introduced Word2Vec and played with it.
I installed and used Numba with Python3.5
I played with wordcloud!
I made a GUI application with Python + PyQt5
[Python] I played with natural language processing ~ transformers ~
I tried Jacobian and partial differential with python
I tried function synthesis and curry with python
I tried fp-growth with python
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
I made blackjack with python!
I compared Java and Python!
Python compilation with pyqt deploy
python with pyenv and venv
I tried gRPC with Python
I tried scraping with python
I made blackjack with Python.
[GUI with Python] PyQt5 -Preparation-
I made wordcloud with Python.
Works with Python and R
[GUI with Python] PyQt5 -Paint-
I want to handle optimization with python and cplex
I implemented collaborative filtering (recommendation) with redis and python
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
[GUI with Python] PyQt5 -Widget II-
I can't install python3 with pyenv-vertualenv
Scraping with Python, Selenium and Chromedriver
I tried web scraping with python.
Scraping with Python and Beautiful Soup
I tried follow management with Twitter API and Python (easy)
I made a fortune with Python.
I sent an SMS with Python
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
[Python] I installed the game from pip and played it
Reading and writing NetCDF with Python
I liked the tweet with python. ..
I played with Mecab (morphological analysis)!
Reading and writing CSV with Python
Multiple integrals with Python and Sympy
I tried to make GUI tic-tac-toe with Python and Tkinter
[GUI with Python] PyQt5 -Custom Widget-
I want to debug with Python
I tried running prolog with python 3.8.2.
This time I learned python III and IV with Prorate
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
I made a daemon with Python
I tried SMTP communication with Python
Sugoroku game and addition game with python
FM modulation and demodulation with Python