[PYTHON] [PyQt x pySerial] Display a list of COM ports connected to the PC in the combo box

At the beginning

combobox.png

Like this, create a combo box with PyQt that allows you to select the COM port connected to your PC. See here for how to create a GUI application using PyQt and Qt Designer.

environment

Installation

$ pip install pyqt5
$ pip install pyserial

① Create UI file

Start Qt Designer and create a nice widget with Combo Box.

designer.png

The name of the Combo Box is "port Combo Box".

port_combo_box_sample.ui


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>219</width>
    <height>70</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QComboBox" name="portComboBox">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>20</y>
     <width>171</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

② Convert UI file to Python file

$ pyuic5 port_combo_box_sample.ui > port_combo_box_sample_ui.py

port_combo_box_sample_ui.py


# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'port_combo_box_sample.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(219, 70)
        self.portComboBox = QtWidgets.QComboBox(Form)
        self.portComboBox.setGeometry(QtCore.QRect(30, 20, 171, 22))
        self.portComboBox.setObjectName("portComboBox")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))

③ Create the main GUI file

port_combo_box_sample_gui.py


import sys

from PyQt5.QtWidgets import QMainWindow, QApplication
from serial.tools import list_ports

from port_combo_box_sample_ui import Ui_Form


class PortComboBoxSampleGui(QMainWindow, Ui_Form):
    def __init__(self, parent=None):
        super(PortComboBoxSampleGui, self).__init__(parent)
        self.setupUi(self)
        self._init_port_combo_box()

    def _init_port_combo_box(self):
        comports = list_ports.comports()
        if not comports:
            print("Port not found")
            return

        for index, comport in enumerate(comports):
            self.portComboBox.addItem(comport.device)


if __name__ == "__main__":
    argvs = sys.argv
    app = QApplication(argvs)
    main_gui = PortComboBoxSampleGui()
    main_gui.show()
    sys.exit(app.exec_())

Recommended Posts

[PyQt x pySerial] Display a list of COM ports connected to the PC in the combo box
[Linux] Command to get a list of commands executed in the past
I want to sort a list in the order of other lists
I tried to display the altitude value of DTM in a graph
Make a copy of the list in Python
I want to see a list of WebDAV files in the Requests module
How to get a list of files in the same directory with python
How to display the modification date of a file in C language up to nanoseconds
How to identify the element with the smallest number of characters in a Python list?
How to check in Python if one of the elements of a list is in another list
How to display the regional mesh of the official statistics window (eStat) in a web browser
Get the value of a specific key up to the specified index in the dictionary list in Python
How to plot the distribution of bacterial composition from Qiime2 analysis data in a box plot
How to pass the execution result of a shell command in a list in Python (non-blocking version)
Get the number of specific elements in a python list
How to display a list of installable versions with pyenv
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
How to connect the contents of a list into a string
How to display a specified column of files in Linux (awk)
How to determine the existence of a selenium element in Python
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
Display the image of the camera connected to the personal computer on the GUI.
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to get the vertex coordinates of a feature in ArcPy
I made a command to display a colorful calendar in the terminal
[Python] A program that rotates the contents of the list to the left
Generate a list packed with the number of days in the current month.
[Python] How to put any number of standard inputs in a list
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Receive a list of the results of parallel processing in Python with starmap
How to format a list of dictionaries (or instances) well in Python
I made a program to check the size of a file in Python
[Python & SQLite] I tried to analyze the expected value of a race with horses in the 1x win range ①
Change the list in a for statement
Various ways to read the last line of a csv file in Python
Feel free to write a test with nose (in the case of + gevent)
How to achieve something like a list of void * (or variant) in Go?
Set the number of elements in a NumPy one-dimensional array to a power of 2 (0 padded)
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
To output a value even in the middle of a cell with Jupyter Notebook
How to count the number of elements in Django and output to a template
I made an appdo command to execute a command in the context of the app
Try to display the Fibonacci sequence in various languages in the name of algorithm practice
I want to set a life cycle in the task definition of ECS
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
Change the data frame of pandas purchase data (id x product) to a dictionary
Python script to get a list of input examples for the AtCoder contest
Try to display the railway data of national land numerical information in 3D