0

I am trying to implement a GUI to my program, so far I created basic GUI with Qt4 designer and converted it, but I don't know how to link the "OK" button to launch my main script in the terminal, also there is a box where I type some random numbers which I want to be send alongside to my main script. Let's say once I launch the PyQt4 script and enter the following numbers "123456" and press the "OK" button, a terminal window (archlinux) will be opened and my main script will be executed alongside with these numbers after it (my main script is called dd.py). This is the PyQt4 code which needs to be edited:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

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()")), Dialog.accept)
        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__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

1 Answer 1

1

In your main script, you need to make it accept arguments. A really simple way to do that is do something like this:

# dd.py
import sys
def main(arg):
    # do something here
    print arg

if __name__ == "__main__":
    arg = sys.argv[1]
    main(arg)

Then in your GUI, you would use the subprocess module to call your main script and pass the argument. So in your button's event handler, you'd do something like this:

subprocess.Popen("/path/to/dd.py", arg)

If you need to be able to pass switches or flags along with arguments, you should read up on argparse or optparse, depending on which version of Python you're using.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Mike, appreciate that you read my message and helped me. Love you man and also your website will be bookmarked in my browser forever :-)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.