Skip to content

Instantly share code, notes, and snippets.

@0x2a94b5
Created July 26, 2020 06:41
Show Gist options
  • Save 0x2a94b5/9fd86004516a38f22c9ce063fbc9b8a2 to your computer and use it in GitHub Desktop.
Save 0x2a94b5/9fd86004516a38f22c9ce063fbc9b8a2 to your computer and use it in GitHub Desktop.
PyQt5 runs system commands and gets the output
#!/usr/bin/env python3
#
# PyQt5 executes commands on the windows system and gets the output.
# Package: pyinstaller -y -F -w pyqt5RunCommandSample.py
# Require: Python==3.6.8 PyQt5==5.15.0 PyInstaller==3.6
#
import re
import sys
import subprocess
from PyQt5.QtWidgets import (QWidget, QToolTip, QPushButton, QApplication, QMessageBox)
from PyQt5.QtGui import QFont
class Ui_Main(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
QToolTip.setFont(QFont('SansSerif', 10))
self.setGeometry(500, 300, 300, 200)
self.setWindowTitle('Command Widget')
btn = QPushButton('GetIP', self)
btn.resize(btn.sizeHint())
btn.move(50, 50)
btn.clicked.connect(self.getOutput)
def getOutput(self):
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
ipconfig = 'C:\\Windows\\System32\\ipconfig.exe'
findstr = 'C:\\Windows\\System32\\findstr.exe'
cmd = '%s | %s IPv4' % (ipconfig, findstr)
message = ''
try:
p = subprocess.Popen(cmd, shell=True, startupinfo=si, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
output, errors = p.communicate(timeout=5)
splitlist = re.split('[\r\n:]', output)
localIP = splitlist[1].strip()
except:
message = 'Command Error, Please Check it.'
message = 'Your IP is %s' % localIP
QMessageBox.information(self, "Tips", message)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = Ui_Main()
mainWindow.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment