Try to bring up a subwindow with PyQt5 and Python

Continuing from previous article, I will play with PyQt5.

This time I will open a sub window

First, output the sub window normally

test.py


#!/usr/bin/env python3

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


class MainWindow(QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        #Creating a button
        makeWindowButton = QPushButton("&make window")
        #Click event(makeWindow())Add
        makeWindowButton.clicked.connect(self.makeWindow)
        #Creating a layout
        layout = QHBoxLayout()
        #Adding buttons to the layout
        layout.addWidget(makeWindowButton)
        #Add layout to window
        self.setLayout(layout)

    def makeWindow(self):
        #Creating a subwindow
        subWindow = SubWindow()
        #Show subwindow
        subWindow.show()

class SubWindow(QWidget):
    def __init__(self, parent=None):
        #Is this the entity of the subwindow? Target. dialog
        self.w = QDialog(parent)
        label = QLabel()
        label.setText('Sub Window')
        layout = QHBoxLayout()
        layout.addWidget(label)
        self.w.setLayout(layout)

    def show(self):
        self.w.exec_()

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

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

Instantiate a subwindow class in the parent window and show () to create a subwindow.

self.w = QDialog(parent) Here and there, it's important.

Without doing this, in the subwindow class self.setLayout(layout) No, it will cause an error.

Output of a subwindow that passes values to the calling class

There are probably situations where you want to output a subwindow and enter parameters. I'll try.

test.py


#!/usr/bin/env python3

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


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

        makeWindowButton = QPushButton("&make window")
        makeWindowButton.clicked.connect(self.makeWindow)

        self.label = QLabel()
        self.label.setText("default:")

        layout = QHBoxLayout()
        layout.addWidget(makeWindowButton)
        layout.addWidget(self.label)
        self.setLayout(layout)

    def makeWindow(self):
        subWindow = SubWindow(self)
        subWindow.show()

    #Run from subwindow
    def setParam(self, param):
        self.label.setText(param)

class SubWindow:
    def __init__(self, parent=None):
        self.w = QDialog(parent)
        self.parent = parent

        label = QLabel()
        label.setText('Sub Window')

        self.edit = QLineEdit()

        button = QPushButton('Send')
        button.clicked.connect(self.setParamOriginal)

        layout = QVBoxLayout()
        layout.addWidget(label)
        layout.addWidget(self.edit)
        layout.addWidget(button)

        self.w.setLayout(layout)

    #I'm passing a value to the parent window here
    def setParamOriginal(self):
        self.parent.setParam(self.edit.text())

    def show(self):
        self.w.exec_()

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

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

I'm putting values from the subwindow to the label of the parent window. It's been 4 days for the first time in Python, and I haven't studied much about the Python language, so I was a little stuck because I didn't know how to handle self. I'm sorry the code is dirty.

Impressions / Discussion

PyQt It's really amazing, it's amazing ... I thought it was very convenient. I'm trying to read the reference, but I can't read it because I'm not fluent in English and I'm not used to Python yet. I have to become a little more Python. .. ..

Finally

Please excuse me because the code is dirty ...

Recommended Posts

Try to bring up a subwindow with PyQt5 and Python
Try to draw a life curve with python
Try to make a "cryptanalysis" cipher with Python
Try to make a dihedral group with Python
Try to make a command standby tool with python
Try to operate DB with Python and visualize with d3
WEB scraping with python and try to make a word cloud from reviews
Introduction and usage of Python bottle ・ Try to set up a simple web server with login function
Try to make it using GUI and PyQt in Python
Try to operate Facebook with Python
I played with PyQt5 and Python3
Just try to receive a webhook in ngrok and python
Try to generate a cyclic peptide from an amino acid sequence with Python and RDKit
How to make a surveillance camera (Security Camera) with Opencv and Python
Try to extract a character string from an image with Python3
I tried to make a periodical process with Selenium and Python
Try to display google map and geospatial information authority map with python
Try adding a wall to your IFC file with IfcOpenShell python
[Python] Try to recognize characters from images with OpenCV and pyocr
Try to reproduce color film with Python
Try logging in to qiita with Python
Fractal to make and play with Python
A memo with Python2.7 and Python3 on CentOS
Try HTML scraping with a Python library
Try drawing a map with python + cartopy 0.18.0
[Cloudian # 3] Try to create a new object storage bucket with Python (boto3)
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
What to do if ipython and python start up with different versions
Try converting latitude / longitude and world coordinates to each other with python
Try to make a capture software with as high accuracy as possible with python (2)
Send mail with mailx to a dummy SMTP server set up with python.
Try to set up a Vim test environment quite seriously (for Python)
Try to solve a set problem of high school math with Python
How to read a CSV file with Python 2/3
Send a message to LINE with Python (LINE Notify)
MessagePack-Try to link Java and Python with RPC
Try to calculate a statistical problem in Python
I made a GUI application with Python + PyQt5
Building a python environment with virtualenv and direnv
Try running Google Chrome with Python and Selenium
Try to solve the man-machine chart with Python
I want to make a game with Python
Try to communicate with EV3 and PC! (MQTT)
Try to automatically generate Python documents with Sphinx
Decide to assign a laboratory with Python (fiction)
Steps to create a Twitter bot with python
Launch a web server with Python and Flask
I want to write to a file with Python
A layman wants to get started with Python
Try to detect fish with python + OpenCV2.4 (unfinished)
Try to solve the traveling salesman problem with a genetic algorithm (Python code)
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
Try to beautify with Talking Head Anime from a Single Image [python preparation]
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Try to use up the Raspberry Pi 2's 4-core CPU with Parallel Python
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Try scraping with Python.
Try making a simple website with responder and sqlite3
How to convert / restore a string with [] in python
Procedure to load MNIST with python and output to png
Try to solve the programming challenge book with python3