Hello, I am creating a GUI program with PyQt4 in python. The purpose is to be an efficiency tool for my company. I have a GUI, but put all the code in one thread. I have not dealt with threads yet and after 2 weeks of reading up, I still can not figure it out with my code. The issue at hand is that I would like to hide the console window while still displaying some outputs to keep track of processes. I was trying to use self.textEdit.append to achieve this, but because it is all in one thread, the outputs all post after the process is done. I know I need threads, I just cant figure out how to apply it to my code.
The gui I got from QTdesigner and added the run method. Could you give me some guidance on how to implement QThread in my program? Any would help. I am at the end of my coding rope. Thank You!
here is my abridged code:
from PyQt4 import QtCore, QtGui
import socket
from binascii import hexlify
import os
import sys, string, telnetlib, time
import csv, datetime, io
import urllib.request
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(595, 252)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tabWidget = QtGui.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(0, 0, 591, 121))
self.tabWidget.setTabPosition(QtGui.QTabWidget.North)
self.tabWidget.setTabShape(QtGui.QTabWidget.Rounded)
self.tabWidget.setElideMode(QtCore.Qt.ElideNone)
self.tabWidget.setUsesScrollButtons(True)
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
.
.
.*GUI Stuff in the tabs..
.
.
.
self.pushButtonRUN = QtGui.QPushButton(self.centralwidget)
self.pushButtonRUN.setGeometry(QtCore.QRect(490, 130, 91, 21))
self.pushButtonRUN.setObjectName(_fromUtf8("pushButtonRUN"))
self.pushButtonRUN.clicked.connect(self.Run)#
self.labelPATH = QtGui.QLabel(self.centralwidget)
self.labelPATH.setGeometry(QtCore.QRect(10, 130, 91, 21))
self.labelPATH.setObjectName(_fromUtf8("labelPATH"))
self.linePATH = QtGui.QLineEdit(self.centralwidget)
self.linePATH.setGeometry(QtCore.QRect(100, 130, 291, 21))
self.linePATH.setObjectName(_fromUtf8("linePATH"))
self.pushButtonPATH = QtGui.QPushButton(self.centralwidget)
self.pushButtonPATH.setGeometry(QtCore.QRect(400, 130, 75, 21))
self.pushButtonPATH.setObjectName(_fromUtf8("pushButtonPATH"))
self.pushButtonPATH.clicked.connect(self.selectPath)#
self.textEdit = QtGui.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(20, 160, 551, 71))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.actionExit = QtGui.QAction(MainWindow)
self.actionExit.setObjectName(_fromUtf8("actionExit"))
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
.
.
.set labels and such
.
def selectPath(self):
self.filedialog = QtGui.QFileDialog()
filepath = str(self.filedialog.getExistingDirectory())
self.linePATH.setText(filepath)
def Run(self):
.
.
.big if/elif statement for each tab performance... example of one below:
elif self.tabWidget.currentIndex() == 1 :
print("Fetching AAA Data...")
AAAIP = self.lineAAAIP.text()
AAAport = self.lineAAAport.text()
AAACMD = self.lineAAACMD.text()
addressAAA = 'http://' + AAAIP + ':' + AAAport +'/' + AAACMD + '.csv'
htmlMRC = urllib.request.urlopen(addressAAA).read()
f = open(self.linePATH.text() + '/' + AAAIP + '-' + AAAport + '.csv', 'wb')
f.write(htmlAAA)
f.close()
print('File saved to ' + self.linePATH.text() + ' as ' + AAAIP + '-' + AAAport + '.csv')
self.textEdit.append(_translate("MainWindow", ">>> Download complete and Files saved to " + self.linePATH.text(), None))###
print("Done.")
.
.
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())