I've been Googling for almost two days now to find an answer about my problem in Python. And I'm totally lost and confused on how am I supposed to solve my problem.
Here's what I want to do. I have a Python file name mymain.py(GUI) and its running and active, I want to change and update the Qlabel text in my GUI by running this command in terminal
sudo python myarg.py -i [any number]
where [number] is user defined. For example I ran this code in the terminal
sudo python myarg.py -i 5
the Qlabel text in GUI should change to 5.
Here's my code:
mymain.py
from PyQt4 import QtCore, QtGui
import PyQt4
import sys
import os
from time import sleep
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(QtGui.QMainWindow):
updatenumber = QtCore.pyqtSignal(int)
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
self.num = 0
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(536, 537)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout_2 = QtGui.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.gridLayout = QtGui.QGridLayout()
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.lblNumber = QtGui.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("DS-Digital"))
font.setPointSize(300)
self.lblNumber.setFont(font)
self.lblNumber.setObjectName(_fromUtf8("lblNumber"))
self.lblNumber.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout.addWidget(self.lblNumber)
self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.lblNumber.setText(QtGui.QApplication.translate("MainWindow", "0", None, QtGui.QApplication.UnicodeUTF8))
self.thread = Thread()
self.thread.update.connect(self.lblNumber.text)
class Thread(QtCore.QThread):
update = QtCore.pyqtSignal(str)
def init(self, parent=app):
QtCore.QThread.init(self,parent)
self.num = ''
def run(self):
self.update.emit(self.num)
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
ex = Ui_MainWindow()
ex.show()
sys.exit(app.exec_())
myarg.py
from mymain import Thread
import sys, getopt
from PyQt4 import QtCore, QtGui
import PyQt4
def main(argv):
ctr = ''
try:
opts, args = getopt.getopt(argv,"hi:",["ifile="])
except getopt.GetoptError:
print 'sigarg.py -i <ctr>'
sys.exit(1)
for opt, arg in opts:
if opt == '-h':
print 'sigarg.py -i <ctr>'
sys.exit()
elif opt in ("-i", "--ifile"):
ctr = arg
m = Thread()
m.num = ctr
m.start()
if __name__ == "__main__":
main(sys.argv[1:])
The version of Python that I'm using is 2.7 and my machine is Raspberry Pi.