Hi guys, still Noob here, so i apologize for not using the correct terminology. I will be as detailed and accurate as possible!
I've been using QT Designer, and been trying to figure out how to "combine" the GUI ive created with the python script I wrote before making a GUI.
I had my .py script made before I even knew about PyQT, QtGUI, and Qt Designer. My understanding on modules, classes, and objects are weak to say the least, as is my understanding on python also.
After visiting http://www.riverbankcomputing.co.uk/software/pyqt/intro I have a much better understanding of PyQT's modules classes and objects (and what a lot of them there are! LOL)
But that has not helped me figure out how to get my gui and program to work together?
To help narrow down my question, I am able to change my .uic to .py and further to .exe figured that out a few days ago, so i really need some help just understanding how to get my GUI buttons to "run/execute" parts of my script.. and pass some variables to it as well. If my thinking is correct, i think i need to make parts of my original script as "objects" in my GUI script?
My project is to build a driver dispatch SMS application. My program will fetch an email from gmail, parse and truncate it, then send the truncated chunks as a text message to a delivery driver as an sms text. The project is in two pieces, one piece is the GUI and the other is the part that gets email, processes it (as mentioned previously) and then sends it through twilio. Both parts work correctly seperately (although, im sure they are not the most efficient and professional looking).
My GUI has two main goals, one is to dispatch email orders to driver, and the other is to provide a regular texting gateway for dispatcher to text back and forth to drivers. I have a set of buttons designated for dispatching drivers who are scheduled to work that day. Then I have a text browser, a text edit, and a set of buttons that are designated for SMS'ing drivers who are scheduled to work that day.
The two things I am trying to figure out are:
1. when a "dispatch employee" button is pressed, I am trying to get the following to happen: A) i want it to run my "get email, process, send text" code and B) pass the employees phone number (im guessing as a variable) to the portion of code that communicates with twilio.
2. with the text edit, text browser and SMS Employee buttons, I would like for the SMS conversation to be displayed when a driver's button is selected, and then for any comments typed into the text edit area to be SMS'd to the driver who is selected when the "Send SMS button" is clicked. (I am going to add the "Send SMS button" as I just thought of that now.)
I will include, both, my GUI code(sorry, it is very long) and my program code... then revisit the question again... (for the millionth time LOL)
GUI code... SMStextdriver.py
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(570, 604)
self.textBrowser = QtGui.QTextBrowser(Form)
self.textBrowser.setGeometry(QtCore.QRect(0, 130, 561, 301))
self.textBrowser.setObjectName(_fromUtf8("textBrowser"))
self.textEdit = QtGui.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(0, 440, 561, 61))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.layoutWidget = QtGui.QWidget(Form)
self.layoutWidget.setGeometry(QtCore.QRect(0, 0, 561, 131))
self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
self.gridLayout = QtGui.QGridLayout(self.layoutWidget)
self.gridLayout.setMargin(0)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.pushButton = QtGui.QPushButton(self.layoutWidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.lcdNumber = QtGui.QLCDNumber(self.layoutWidget)
self.lcdNumber.setObjectName(_fromUtf8("lcdNumber"))
self.gridLayout.addWidget(self.lcdNumber, 0, 1, 1, 1)
self.line_2 = QtGui.QFrame(self.layoutWidget)
self.line_2.setFrameShape(QtGui.QFrame.VLine)
self.line_2.setFrameShadow(QtGui.QFrame.Sunken)
self.line_2.setObjectName(_fromUtf8("line_2"))
self.gridLayout.addWidget(self.line_2, 0, 2, 3, 1)
self.pushButton_4 = QtGui.QPushButton(self.layoutWidget)
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
self.gridLayout.addWidget(self.pushButton_4, 0, 3, 1, 1)
self.lcdNumber_6 = QtGui.QLCDNumber(self.layoutWidget)
self.lcdNumber_6.setObjectName(_fromUtf8("lcdNumber_6"))
self.gridLayout.addWidget(self.lcdNumber_6, 0, 4, 1, 1)
self.pushButton_2 = QtGui.QPushButton(self.layoutWidget)
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.gridLayout.addWidget(self.pushButton_2, 1, 0, 1, 1)
self.lcdNumber_2 = QtGui.QLCDNumber(self.layoutWidget)
self.lcdNumber_2.setObjectName(_fromUtf8("lcdNumber_2"))
self.gridLayout.addWidget(self.lcdNumber_2, 1, 1, 1, 1)
self.pushButton_5 = QtGui.QPushButton(self.layoutWidget)
self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
self.gridLayout.addWidget(self.pushButton_5, 1, 3, 1, 1)
self.lcdNumber_5 = QtGui.QLCDNumber(self.layoutWidget)
self.lcdNumber_5.setObjectName(_fromUtf8("lcdNumber_5"))
self.gridLayout.addWidget(self.lcdNumber_5, 1, 4, 1, 1)
self.pushButton_3 = QtGui.QPushButton(self.layoutWidget)
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.gridLayout.addWidget(self.pushButton_3, 2, 0, 1, 1)
self.lcdNumber_3 = QtGui.QLCDNumber(self.layoutWidget)
self.lcdNumber_3.setObjectName(_fromUtf8("lcdNumber_3"))
self.gridLayout.addWidget(self.lcdNumber_3, 2, 1, 1, 1)
self.pushButton_6 = QtGui.QPushButton(self.layoutWidget)
self.pushButton_6.setObjectName(_fromUtf8("pushButton_6"))
self.gridLayout.addWidget(self.pushButton_6, 2, 3, 1, 1)
self.lcdNumber_4 = QtGui.QLCDNumber(self.layoutWidget)
self.lcdNumber_4.setObjectName(_fromUtf8("lcdNumber_4"))
self.gridLayout.addWidget(self.lcdNumber_4, 2, 4, 1, 1)
self.line = QtGui.QFrame(self.layoutWidget)
self.line.setFrameShape(QtGui.QFrame.HLine)
self.line.setFrameShadow(QtGui.QFrame.Sunken)
self.line.setObjectName(_fromUtf8("line"))
self.gridLayout.addWidget(self.line, 3, 0, 1, 5)
self.pushButton_15 = QtGui.QPushButton(Form)
self.pushButton_15.setGeometry(QtCore.QRect(480, 570, 84, 25))
self.pushButton_15.setObjectName(_fromUtf8("pushButton_15"))
self.widget = QtGui.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(0, 510, 176, 89))
self.widget.setObjectName(_fromUtf8("widget"))
self.gridLayout_2 = QtGui.QGridLayout(self.widget)
self.gridLayout_2.setMargin(0)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.pushButton_7 = QtGui.QPushButton(self.widget)
self.pushButton_7.setObjectName(_fromUtf8("pushButton_7"))
self.gridLayout_2.addWidget(self.pushButton_7, 0, 0, 1, 1)
self.pushButton_10 = QtGui.QPushButton(self.widget)
self.pushButton_10.setObjectName(_fromUtf8("pushButton_10"))
self.gridLayout_2.addWidget(self.pushButton_10, 0, 1, 1, 1)
self.pushButton_8 = QtGui.QPushButton(self.widget)
self.pushButton_8.setObjectName(_fromUtf8("pushButton_8"))
self.gridLayout_2.addWidget(self.pushButton_8, 1, 0, 1, 1)
self.pushButton_12 = QtGui.QPushButton(self.widget)
self.pushButton_12.setObjectName(_fromUtf8("pushButton_12"))
self.gridLayout_2.addWidget(self.pushButton_12, 1, 1, 1, 1)
self.pushButton_9 = QtGui.QPushButton(self.widget)
self.pushButton_9.setObjectName(_fromUtf8("pushButton_9"))
self.gridLayout_2.addWidget(self.pushButton_9, 2, 0, 1, 1)
self.pushButton_11 = QtGui.QPushButton(self.widget)
self.pushButton_11.setObjectName(_fromUtf8("pushButton_11"))
self.gridLayout_2.addWidget(self.pushButton_11, 2, 1, 1, 1)
self.widget1 = QtGui.QWidget(Form)
self.widget1.setGeometry(QtCore.QRect(240, 510, 187, 91))
self.widget1.setObjectName(_fromUtf8("widget1"))
self.gridLayout_3 = QtGui.QGridLayout(self.widget1)
self.gridLayout_3.setMargin(0)
self.gridLayout_3.setObjectName(_fromUtf8("gridLayout_3"))
self.pushButton_13 = QtGui.QPushButton(self.widget1)
self.pushButton_13.setObjectName(_fromUtf8("pushButton_13"))
self.gridLayout_3.addWidget(self.pushButton_13, 0, 0, 1, 1)
self.pushButton_14 = QtGui.QPushButton(self.widget1)
self.pushButton_14.setObjectName(_fromUtf8("pushButton_14"))
self.gridLayout_3.addWidget(self.pushButton_14, 1, 0, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Form", "Dispatch Emp 1", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_4.setText(QtGui.QApplication.translate("Form", "Dispatch Emp 4", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_2.setText(QtGui.QApplication.translate("Form", "Dispatch Emp 2", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_5.setText(QtGui.QApplication.translate("Form", "Dispatch Emp 5", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_3.setText(QtGui.QApplication.translate("Form", "Dispatch Emp 3", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_6.setText(QtGui.QApplication.translate("Form", "Dispatch Emp 6", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_15.setText(QtGui.QApplication.translate("Form", "End of Day", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_7.setText(QtGui.QApplication.translate("Form", "SMS Emp 1", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_10.setText(QtGui.QApplication.translate("Form", "SMS Emp 4", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_8.setText(QtGui.QApplication.translate("Form", "SMS Emp 2", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_12.setText(QtGui.QApplication.translate("Form", "SMS Emp 5", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_9.setText(QtGui.QApplication.translate("Form", "SMS Emp 3", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_11.setText(QtGui.QApplication.translate("Form", "SMS Emp 6", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_13.setText(QtGui.QApplication.translate("Form", "SMS DRIVER", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_14.setText(QtGui.QApplication.translate("Form", "SMS DRIVER 40 Minute Warning", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
and here is my "get, process, and send text.py" script (Still, tweaking it to clean up, and make more efficient over time as i learn)
import re
import sys
import time
import email
import quopri
import imaplib
from twilio.rest import TwilioRestClient
# GET MAIL
def extract_body(payload):
if isinstance(payload,str):
return payload
else:
return '\n'.join([extract_body(part.get_payload()) for part in payload])
conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login("GMAIL USER ID HERE", "GMAIL Password here")
conn.select()
typ, data = conn.search(None, 'UNSEEN')
try:
for num in data[0].split():
typ, msg_data = conn.fetch(num, '(RFC822)')
for response_part in msg_data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
subject=msg['subject'] #not necessary
print(subject) #not necessary
payload=msg.get_payload()
body=extract_body(payload)
print(body) #not necessary
removed=quopri.decodestring(body) #trying without
stripped=re.sub('<[^<]+?>', '',removed) #trying without
print(removed) #changed from stripped to try without
# head, sep, tail = removed.partition('subject line.') #needs to be examined
file=open("c:\\neworderup.txt", "w")
file.write(removed) #changed from body writes file of cleaned and parsed data
file.close()
typ, response = conn.store(num, '+FLAGS', r'(\Seen)')
finally:
try:
conn.close()
except:
pass
conn.logout()
# Need to experiment with quopri, could probably remove one instance of it.
# Need to experiment with sub, could probably be removed also.
file1=open("c:\\neworderup.txt", "r")
toremove = file1.read()
toquo=re.sub('<[^<]+?>', '', toremove)
removed=quopri.decodestring(toquo)
string=removed
file1.close()
split=string.split()
joined=" ".join(string.split()) # removes xtra WS and empty lines
parsed=joined
#processed=parsed[parsed.index("Rate:"):parsed.index("Best,")] #parses string... no longer needed here.
final = re.sub(r"\Rate:\s?","",parsed) #changed from processed
file2=open("c:\\neworderupstep2.txt", "w")
file2.write(final) #writes file of cleaned and parsed data
file2.close()
file3 = open('C:\\neworderupstep2.txt') # this section chunks the
chunks = [] # file to 120 character lengths
while "file is not empty":
chunk = file3.read(110)
if not chunk:
break
chunks.append(chunk)
file3.close()
# writes each chunk to own file and SMS's it
for i,name in enumerate(chunks):
file4 = open("C:\\neworderupchunked"+str(i+1)+".txt","w")
file4.write(name+"\n")
account_sid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Find these values at
auth_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # [url]https://twilio.com/user/account[/url]
client = TwilioRestClient(account_sid, auth_token)
message = client.sms.messages.create(to="Employee Phone Number VAriable Here", from_="Twilio Number Here",
body="%s" % (name+"\n"))
file4.close()
time.sleep(1.0)
exit()
Sorry for the long ugly code! :)
I don't need everything coded in, if someone could just write one of the dispatch button click actions for me, I can add the rest, and probably even adapt it for the SMStext buttons... but im also wondering how to get the driver/dispatch conversation displayed.. probably save to file, then open and append whenever a text between driver and dispatch happens.
Hope some of this makes sense..
thanks in advance for any help, advice, code! Cheers!