#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0

    PyQt4 module to execute another script in a terminal


    Hello everyone,

    I am trying to implement a GUI to my program. When I type some numbers in the box and press the "ok" button (in the PyQt4 scirpt), dd.py (my main script) is executed in the background, what I want is to open a terminal window with it and process it there, because right now it says that I just executed dd.py without any numbers after it (how to get the input from the PyQt4 script and add it to my dd.py once it's called ? )
    I can launch the script manually via: python dd.py 123456 , the numbers will be processed by the script, but the purpose of the PyQt4 GUI is to ease that process.

    qt4.py

    Code:
    #!/usr/bin/env python
    from PyQt4 import QtCore, QtGui
    import sys
    import os
    import subprocess
    
    
    try:
        _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
        def _fromUtf8(s):
            return s
    
    
    def butsend():
        subprocess.Popen("/home/frost/Desktop/dd.py")
    
    try:
        _encoding = QtGui.QApplication.UnicodeUTF8
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig, _encoding)
    except AttributeError:
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig)
    
    class Ui_Dialog(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName(_fromUtf8("Dialog"))
            Dialog.resize(400, 79)
            self.buttonBox = QtGui.QDialogButtonBox(Dialog)
            self.buttonBox.setGeometry(QtCore.QRect(50, 30, 341, 32))
            self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
            self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
            self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
            self.lineEdit = QtGui.QLineEdit(Dialog)
            self.lineEdit.setGeometry(QtCore.QRect(10, 30, 161, 31))
            self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
    
            self.retranslateUi(Dialog)
            QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), butsend)
            QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
    
        def retranslateUi(self, Dialog):
            Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
    
    
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        Dialog = QtGui.QDialog()
        ui = Ui_Dialog()
        ui.setupUi(Dialog)
        Dialog.show()
        sys.exit(app.exec_())
    dd.py (my main script that processes the typed numbers: pyton dd.py 123456)

    Code:
    #!/usr/bin/env python
    # touchspeedcalc_test.py (2009/10/09)
    # Example for demonstrating use of SpeedTouch wireless router default password lookup tables
    #
    #   http://www.mentalpitstop.com/touchspeedcalc
    #
    # Distributed under the Creative Commons Attribution Non-commercial Share-Alike license,
    # http://creativecommons.org/licenses/by-nc-sa/3.0/us/
    # Contact me at mentalpitstop.com for more information and licensing info.
    #
    # Make sure the correct modules (like hashlib) are installed in your python version, as I've noticed some python versions (2.4 vs 2.6) handle this differently
    # Created and tested with Python 2.6
    # Note that the data files must be located in the /data/ subdirectory for this script to find them.
    #
    # Usage:
    #   python2.6 touchspeedcalc_test.py [SSID]
    # Where [SSID] are the last 6 characters of the SSID.
    #
    # Edit the 'YEARS' variable if you want to include 2010 (not tested)
    
    import hashlib
    import sys
    import binascii
    import re
    
    
    #SSIDEND = "1234AB" #example
    if len(sys.argv) < 2:
    
      sys.exit()
    
    SSIDEND = sys.argv[1].upper()
    #sys.argv[1].decode("hex")
    
    
    if len(SSIDEND) == 6:
      #SpeedTouch:
      FINDPOS = 0
    elif len(SSIDEND) == 4:
      #BT HomeHub:
      FINDPOS = 1
    else:
      print "SSID-end must be either 6 or 4 characters."
      sys.exit()
    
    YEARS = [2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039, 2040, 2041, 2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2050, 2051, 2052, 2053, 2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061, 2062, 2063, 2064, 2065, 2066, 2067, 2068, 2069, 2070, 2071, 2072, 2073, 2074, 2075, 2076, 2077, 2078, 2079, 2080, 2081, 2082, 2083, 2084, 2085, 2086, 2087, 2088, 2089, 2090, 2091, 2092, 2093, 2094, 2095, 2096, 2097, 2098, 2099, 2100]
    #YEARS = [ 2009, 2008, 2007, 2006, 2005, 2004, 2010 ]
    #YEAR = sys.argv[2].lower()
    
    def ascii2hex(char):
      return hex(ord(char))[2:].upper()
    
    CHARSET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    BINCODE = binascii.unhexlify("".join(SSIDEND.split()))
    #print "Compressed SSID: ", BINCODE
    
    
    for YEAR in YEARS:
      FILE = "db/db_with_keys___" + str(YEAR) + ".dat"
      INFILE = open(FILE,"rb")
      FILEDATA = INFILE.read()
      INFILE.close()
      WHEREFOUND = FILEDATA.find(BINCODE, 0)
      while (WHEREFOUND > -1):
        if WHEREFOUND % 3 == FINDPOS:
          PRODIDNUM = (WHEREFOUND / 3) % (36*36*36)
          PRODWEEK = (WHEREFOUND / 3) / (36*36*36) +1
          PRODID1 = PRODIDNUM / (36*36)
          PRODID2 = (PRODIDNUM / 36) % 36
          PRODID3 = PRODIDNUM % 36
          SERIAL = 'CP%02d%02d%s%s%s' % (YEAR-2000,PRODWEEK,ascii2hex(CHARSET[PRODID1:PRODID1+1]),ascii2hex(CHARSET[PRODID2:PRODID2+1]),ascii2hex(CHARSET[PRODID3:PRODID3+1]))
          SHA1SUM = hashlib.sha1(SERIAL).digest().encode("hex").upper()
          SSID = SHA1SUM[-6:]
          ACCESSKEY = SHA1SUM[0:10]
          if len(SSIDEND) == 4:
            # BT HomeHub password is lowercase:
            ACCESSKEY = ACCESSKEY.lower()
          #print "Serial number:", str(SERIAL)  # does not give proper serial number for the thompsons BT series
          #print "SHA1SUM:", str(SHA1SUM)
          print "", str(ACCESSKEY)
    
        WHEREFOUND = FILEDATA.find(BINCODE, WHEREFOUND+1)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    dd.py requires an argument, therefor must fail when invoked by butsend. I'd recreate butsend as a method of Ui_Dialog and then find a clever way to pass arguments to dd.py, maybe as an attribute of the object.
    Code:
    def butsend(self):
        my_pipe = subprocess.Popen("/home/frost/Desktop/dd.py "+self.argument,shell=True)
        return my_pipe.read()
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0
    Originally Posted by b49P23TIvg
    dd.py requires an argument, therefor must fail when invoked by butsend. I'd recreate butsend as a method of Ui_Dialog and then find a clever way to pass arguments to dd.py, maybe as an attribute of the object.
    Code:
    def butsend(self):
        my_pipe = subprocess.Popen("/home/frost/Desktop/dd.py "+self.argument,shell=True)
        return my_pipe.read()
    Thank you so much for second time :-)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0
    @b49P23TIvg ,
    Take a look at the video: https://docs.google.com/file/d/0BzLYQvoKelmETHlBa0FIWGNhcTA/edit?usp=sharing , this is what it should do, but in this demo I added earlier these numbers in the qt4.py script. I want the input to be sent over dd.py and be processed from it, without the need to add manually random numbers in it anymore.

    Code:
    def butsend():
        subprocess.call(["xfce4-terminal", "-H", "-x", "python2", "dd.py", "26a778"])

IMN logo majestic logo threadwatch logo seochat tools logo