I'm new in python and I want to know the way to improve this code with best practices.

I'm using PyQT5 to create new thread In that thread I'm using scapy to sniff traffic and filter ARP traffic to find devices which are broadcasting the network.

In the complete code I'm filtering IP traffic too, but now I'm working on threads in order that GUI doesn't get freeze.

Here the code:

from PyQt5 import QtGui
from PyQt5.QtWidgets import * # QApplication, QDialog, QProgressBar, QPushButton, QVBoxLayout
import sys
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QRect
from scapy.all import sniff, getmacbyip, ARP, get_if_addr, sr
from time import time, sleep

def scan_network(ProgressBar, sec):

  dict_scan = {}
  start_time = time()
  print (dict_scan)
def packet_deploy(change_value: pyqtSignal, start_time: float, dict_scan: dict, sec: int):
  def upload_packet(packet):
    if packet.haslayer("ARP"):
    # upload packet, using passed arguments
      dict_scan[(packet[ARP].hwsrc).replace(":", "").upper()] = packet[ARP].psrc
      LoadingBar(change_value, start_time, sec)
  return upload_packet
def LoadingBar(change_value, start_time, sec):
  ellapsed =  int(((time() - start_time)*100) / sec)
class MyThread(QThread):
    def __init__(self, myvar, parent = None):
        self.myvar = myvar[0]
    change_value = pyqtSignal(int)
    def run(self):
        seconds = self.myvar.txt_From.toPlainText()
        print (seconds)
        sec = 10
        dict_scan = {}
        start_time = time()
        sniff(prn=packet_deploy(self.change_value, start_time, dict_scan, sec), timeout = (sec), store=0)
        print (dict_scan)
class Window(QDialog):
    def __init__(self):
        self.title = "PyQt5 ProgressBar"
        self.top = 200
        self.left = 500
        self.width = 300
        self.height = 200
        self.setGeometry(self.left, self.top, self.width, self.height)
        vbox = QVBoxLayout()
        self.progressbar = QProgressBar()
        self.progressbar.setStyleSheet("QProgressBar {border: 2px solid grey;border-radius:8px;padding:1px}"
                                       "QProgressBar::chunk {background:yellow}")
        self.button = QPushButton("Start Progressbar")
        self.txt_From = QTextEdit()
        self.txt_From.setGeometry(QRect(0, 0, 151, 21))
    def startProgressBar(self):
        self.thread = MyThread(myvar=[self])
    def setProgressVal(self, val):
App = QApplication(sys.argv)
window = Window()
  • \$\begingroup\$ Welcome to the Code Review Community. The title of the question should indicate what the code does not what your concerns about the code are. Please read How do I ask a good question. As part of a good code review we will sometimes mention best practices, but asking specifically about best practices will sometimes be considered off-topic. I do hope you get some good answers. \$\endgroup\$
    – pacmaninbw
    Commented Jul 4, 2021 at 13:02

1 Answer 1


If your GUI is freezing then you are working your CPU or Memory too much.

I don't know intended usage of the App, but it might be worth considering splitting this script into two:

  1. First script will collect data.
  2. Second script will displays data.

Data collection can be handled in a separate thread or run on a specific core. Data can also be written into a file or some other data store. The display script will only focus on reading & displaying data and will have sufficient resources not to lag.

  • \$\begingroup\$ In this case I find the solution, it was a thread problem. When you work with PyQT5 you need to use qthread to avoid GUI freezing \$\endgroup\$ Commented Jul 7, 2021 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.