Python: Pyqt5, show multiple message box, how to show one?

Abunis Source

I write this code for pop up a message box, if the process i write into the textbox is active. But if i insert for example "chrome.exe" he pop up a lot of message box, because in my process list are 10 chrome.exe process. Same with the other process, if there are only one process then he pop up only one. How can i fix this? There is the code i write:

import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, 
QLineEdit, QMessageBox
import psutil


class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 textbox - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 400
        self.height = 140
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # Create textbox
        self.textbox = QLineEdit(self)
        self.textbox.setGeometry(QtCore.QRect(30, 40, 161, 31))
        self.textbox.setObjectName("textEdit")

        # Create a button in the window
        self.button = QPushButton('Show text', self)
        self.button.move(20, 80)

        # connect button to function on_click
        self.button.clicked.connect(self.on_click)
        self.show()

    def on_click(self):
        textboxValue = self.textbox.text()
        for pid in psutil.pids():  # Controlla se il processo è attivo
            try:
                listapid = psutil.Process(pid)
                if listapid.name() == textboxValue:
                    QMessageBox.about(self, 'Process', 'Processo trovato')
                    self.textbox.setText("")
            except:
                pass

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

Answers

answered 6 months ago eyllanesc #1

It is well known that chrome creates multiple processes, so you get that behavior, a possible solution is to use break when there is a match.

def on_click(self):
    textboxValue = self.textbox.text()
    for pid in psutil.pids():  # Controlla se il processo è attivo
        try:
            listapid = psutil.Process(pid)
            if listapid.name() == textboxValue:
                QMessageBox.about(self, 'Process', 'Processo trovato')
                self.textbox.clear()
                break
        except:
            pass

comments powered by Disqus