How should I call a QThread
that is defined in another python file?
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):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
self.showMaximized()
MainWindow.setStyleSheet(_fromUtf8("background-color: rgb(0, 0, 0);"))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.gridLayout = QtGui.QGridLayout()
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem, 2, 0, 1, 1)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.lblNowServing = QtGui.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Bebas Neue"))
font.setPointSize(140)
self.lblNowServing.setFont(font)
self.lblNowServing.setStyleSheet(_fromUtf8("color: rgb(170, 0, 0);"))
self.lblNowServing.setObjectName(_fromUtf8("lblNowServing"))
self.lblNowServing.setAlignment(QtCore.Qt.AlignCenter)
self.verticalLayout.addWidget(self.lblNowServing)
self.lblNowServingNumber = QtGui.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("DS-Digital"))
font.setPointSize(350)
self.lblNowServingNumber.setFont(font)
self.lblNowServingNumber.setStyleSheet(_fromUtf8("color: rgb(170, 0, 0);"))
self.lblNowServingNumber.setObjectName(_fromUtf8("lblNowServingNumber"))
self.lblNowServingNumber.setAlignment(QtCore.Qt.AlignCenter)
self.verticalLayout.addWidget(self.lblNowServingNumber)
self.horizontalLayout_2.addLayout(self.verticalLayout)
self.verticalLayout_2 = QtGui.QVBoxLayout()
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.lblCounter = QtGui.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Bebas Neue"))
font.setPointSize(140)
self.lblCounter.setFont(font)
self.lblCounter.setStyleSheet(_fromUtf8("color: rgb(170, 0, 0);"))
self.lblCounter.setObjectName(_fromUtf8("lblCounter"))
self.lblCounter.setAlignment(QtCore.Qt.AlignCenter)
self.verticalLayout_2.addWidget(self.lblCounter)
self.lblCounterNumber = QtGui.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("DS-Digital"))
font.setPointSize(350)
self.lblCounterNumber.setFont(font)
self.lblCounterNumber.setStyleSheet(_fromUtf8("color: rgb(170, 0, 0);"))
self.lblCounterNumber.setObjectName(_fromUtf8("lblCounterNumber"))
self.lblCounterNumber.setAlignment(QtCore.Qt.AlignCenter)
self.verticalLayout_2.addWidget(self.lblCounterNumber)
self.horizontalLayout_2.addLayout(self.verticalLayout_2)
self.gridLayout.addLayout(self.horizontalLayout_2, 0, 0, 1, 1)
spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem, 1, 0, 1, 1)
self.horizontalLayout.addLayout(self.gridLayout)
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.lblNowServing.setText(QtGui.QApplication.translate("MainWindow", " NOW SERVING ", None, QtGui.QApplication.UnicodeUTF8))
self.lblCounter.setText(QtGui.QApplication.translate("MainWindow", "COUNTER", None, QtGui.QApplication.UnicodeUTF8))
self.lblCounterNumber.setText(str(1))
self.lblNowServingNumber.setText(str(1))
class valChange(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
self.myctr = 0
self.myval = 0
self.lblNowServingNumber.setText(str(myval))
self.lblCounterNumber.setText(str(myctr))
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
ex = Ui_MainWindow()
ex.show()
sys.exit(app.exec_())
exarg.py
from PyQt4 import QtCore, QtGui
import mymain
import sys, getopt
def main(argv):
ctr = ''
val = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'test.py -i <ctr> -o <val>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <ctr> -o <val>'
sys.exit()
elif opt in ("-i", "--ifile"):
ctr = arg
elif opt in ("-o", "--ofile"):
val = arg
m = mymain.valChange()
m.myctr = ctr
m.myval = val
m.start()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main(sys.argv[1:])
this is what i did, I run the mymain.py in shell. and i run the exarg.py in terminal/command line using this command
sudo python exarg.py -i 3 -o 4
but I always got an error saying:
'QThread: Destroyed while thread is still running'
any comments/suggestions would be highly appreciated. thanks.