python3.7 - how to open a new thread and close the old each click on a button?

alon.najman at gmail.com alon.najman at gmail.com
Sat Sep 15 12:54:11 EDT 2018


hii all,
python3.7 - how to open a new thread and close the old each click on a button?
here is my code:


    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'AlonStockMarket.ui'
    #
    # Created by: PyQt5 UI code generator 5.11.2
    #
    # WARNING! All changes made in this file will be lost!
    
    
    import time
    import sys
    import requests
    from lxml import html
    import requests
    import urllib.request, urllib.error, urllib.parse
    import _thread
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    import threading 
    global flag
    global loopexit
    
    flag=0
    loopexit=0
    
    def get_quote(str):
        url="https://api.iextrading.com/1.0/stock/"+str+"/price"
        resource = urllib.request.urlopen(url)
        content =  resource.read().decode(resource.headers.get_content_charset())
        print (content)
        content=float(content)
        return content
    
    def myfunction():
         global flag
         global loopexit
         print ("Executing myfunction in thread: ")
         print("hello, world")
         while loopexit!=1:
                if (loopexit==1):
                   break
                time.sleep(15)
                f=open("flag.txt", "r")
                if f.mode == 'r': 
                    flag =f.read()
                timeHour = int(time.strftime('%H'))
                print("keep alive")
                print (time.strftime('%H:%M:%S'))
                while ((timeHour>16) and (timeHour<23) and loopexit!=1):
                    if (loopexit==1):
                         break
                    print(time.strftime('%H:%M:%S'))
                    price=get_quote(UserSymbol)
                    time.sleep(5)
                    if flag!=time.strftime("%d/%m/%Y") and price>UserStockPrice and (RadioButtonAbove==True):
                         print ("send SMS")
                         import requests
                         requests.post('https://textbelt.com/text', {
                         'phone': '+972541234567',
                         'message': "Hi Alon Najman,EVOK value: "+price,
                         'key': 'secret',
                         }) 
                         #write to file
                         with open("flag.txt", "w") as text_file:
                             text_file.write(format(time.strftime("%d/%m/%Y")))
                         #read from file
                         f=open("flag.txt", "r")
                         if f.mode == 'r': 
                           flag =f.read()
    
                         if flag!=time.strftime("%d/%m/%Y") and price<UserStockPrice and (RadioButtonBellow==True):
                             print ("send SMS")
                             import requests
                             requests.post('https://textbelt.com/text', {
                             'phone': '+972546233257',
                             'message': "Hi Alon Najman,EVOK value: "+price,
                             'key': 'secret',
                             }) 
                             #write to file
                             with open("flag.txt", "w") as text_file:
                                 text_file.write(format(time.strftime("%d/%m/%Y")))
                             #read from file
                             f=open("flag.txt", "r")
                             if f.mode == 'r': 
                               flag =f.read()  
    
    
      
    
     
    
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
     
    class Ui_Dialog(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName("Dialog")
            Dialog.resize(666, 571)
            self.radioButton = QtWidgets.QRadioButton(Dialog)
            self.radioButton.setGeometry(QtCore.QRect(80, 190, 191, 24))
            font = QtGui.QFont()
            font.setPointSize(14)
            self.radioButton.setFont(font)
            self.radioButton.setObjectName("radioButton")
            self.buttonGroup_2 = QtWidgets.QButtonGroup(Dialog)
            self.buttonGroup_2.setObjectName("buttonGroup_2")
            self.buttonGroup_2.addButton(self.radioButton)
            self.checkBox = QtWidgets.QCheckBox(Dialog)
            self.checkBox.setGeometry(QtCore.QRect(290, 450, 131, 17))
            font = QtGui.QFont()
            font.setPointSize(14)
            self.checkBox.setFont(font)
            self.checkBox.setObjectName("checkBox")
            self.checkBox_2 = QtWidgets.QCheckBox(Dialog)
            self.checkBox_2.setGeometry(QtCore.QRect(290, 480, 141, 17))
            font = QtGui.QFont()
            font.setPointSize(14)
            self.checkBox_2.setFont(font)
            self.checkBox_2.setObjectName("checkBox_2")
            self.radioButton_2 = QtWidgets.QRadioButton(Dialog)
            self.radioButton_2.setGeometry(QtCore.QRect(300, 190, 186, 24))
            font = QtGui.QFont()
            font.setPointSize(14)
            self.radioButton_2.setFont(font)
            self.radioButton_2.setObjectName("radioButton_2")
            self.buttonGroup_2.addButton(self.radioButton_2)
            self.pushButton = QtWidgets.QPushButton(Dialog)
            self.pushButton.setGeometry(QtCore.QRect(60, 460, 72, 31))
            font = QtGui.QFont()
            font.setPointSize(14)
            self.pushButton.setFont(font)
            self.pushButton.setObjectName("pushButton")
            self.radioButton_3 = QtWidgets.QRadioButton(Dialog)
            self.radioButton_3.setGeometry(QtCore.QRect(80, 250, 191, 24))
            font = QtGui.QFont()
            font.setPointSize(14)
            self.radioButton_3.setFont(font)
            self.radioButton_3.setObjectName("radioButton_3")
            self.buttonGroup = QtWidgets.QButtonGroup(Dialog)
            self.buttonGroup.setObjectName("buttonGroup")
            self.buttonGroup.addButton(self.radioButton_3)
            self.radioButton_4 = QtWidgets.QRadioButton(Dialog)
            self.radioButton_4.setGeometry(QtCore.QRect(300, 250, 186, 24))
            font = QtGui.QFont()
            font.setPointSize(14)
            self.radioButton_4.setFont(font)
            self.radioButton_4.setObjectName("radioButton_4")
            self.buttonGroup.addButton(self.radioButton_4)
            self.pushButton_2 = QtWidgets.QPushButton(Dialog)
            self.pushButton_2.setGeometry(QtCore.QRect(30, 20, 72, 31))
            font = QtGui.QFont()
            font.setPointSize(14)
            self.pushButton_2.setFont(font)
            self.pushButton_2.setObjectName("pushButton_2")
            self.textEdit_4 = QtWidgets.QTextEdit(Dialog)
            self.textEdit_4.setGeometry(QtCore.QRect(310, 383, 221, 38))
            font = QtGui.QFont()
            font.setPointSize(16)
            self.textEdit_4.setFont(font)
            self.textEdit_4.setObjectName("textEdit_4")
            self.textEdit_5 = QtWidgets.QTextEdit(Dialog)
            self.textEdit_5.setGeometry(QtCore.QRect(310, 340, 221, 38))
            font = QtGui.QFont()
            font.setPointSize(16)
            self.textEdit_5.setFont(font)
            self.textEdit_5.setObjectName("textEdit_5")
            self.label = QtWidgets.QLabel(Dialog)
            self.label.setGeometry(QtCore.QRect(150, 80, 111, 16))
            font = QtGui.QFont()
            font.setPointSize(14)
            self.label.setFont(font)
            self.label.setObjectName("label")
            self.label_2 = QtWidgets.QLabel(Dialog)
            self.label_2.setGeometry(QtCore.QRect(410, 80, 131, 16))
            font = QtGui.QFont()
            font.setPointSize(14)
            self.label_2.setFont(font)
            self.label_2.setObjectName("label_2")
            self.label_3 = QtWidgets.QLabel(Dialog)
            self.label_3.setGeometry(QtCore.QRect(160, 350, 121, 20))
            font = QtGui.QFont()
            font.setPointSize(14)
            self.label_3.setFont(font)
            self.label_3.setObjectName("label_3")
            self.label_4 = QtWidgets.QLabel(Dialog)
            self.label_4.setGeometry(QtCore.QRect(140, 390, 171, 20))
            font = QtGui.QFont()
            font.setPointSize(14)
            self.label_4.setFont(font)
            self.label_4.setObjectName("label_4")
            self.splitter = QtWidgets.QSplitter(Dialog)
            self.splitter.setGeometry(QtCore.QRect(80, 120, 512, 31))
            self.splitter.setOrientation(QtCore.Qt.Horizontal)
            self.splitter.setObjectName("splitter")
            self.textEdit_2 = QtWidgets.QTextEdit(self.splitter)
            self.textEdit_2.setObjectName("textEdit_2")
            self.textEdit_3 = QtWidgets.QTextEdit(self.splitter)
            self.textEdit_3.setObjectName("textEdit_3")
            self.label_5 = QtWidgets.QLabel(Dialog)
            self.label_5.setGeometry(QtCore.QRect(170, 30, 351, 31))
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(18)
            self.label_5.setFont(font)
            self.label_5.setObjectName("label_5")
            self.pushButton_3 = QtWidgets.QPushButton(Dialog)
            self.pushButton_3.setGeometry(QtCore.QRect(150, 460, 72, 31))
            font = QtGui.QFont()
            font.setPointSize(14)
            self.pushButton_3.setFont(font)
            self.pushButton_3.setObjectName("pushButton_3")
            self.retranslateUi(Dialog)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
            self.pushButton.clicked.connect(self.printMessage)
            self.radioButton.toggled.connect(self.myradioButton1_function)
            self.radioButton_2.toggled.connect(self.myradioButton2_function)
           
            
            
        def myradioButton1_function(self):
            if self.radioButton.isChecked():
                    print ('myradioButton1 is Checked')    
    
        def myradioButton2_function(self):
            if self.radioButton_2.isChecked():
                    print ('myradioButton2 is Checked')
    
        def printMessage(self):
             global UserSymbol
             global UserStockPrice
             UserSymbol=self.textEdit_2.toPlainText()
             UserSymbol=str(UserSymbol)
             print (UserSymbol)
             UserStockPrice=self.textEdit_3.toPlainText()
             UserStockPrice=float(UserStockPrice)
             t = threading.Thread(target=myfunction)
             t.start()
                        
    
        def retranslateUi(self, Dialog):
            _translate = QtCore.QCoreApplication.translate
            Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
            self.radioButton.setText(_translate("Dialog", "Above Price (x>)"))
            self.checkBox.setText(_translate("Dialog", "SMS ALERT"))
            self.checkBox_2.setText(_translate("Dialog", "GMAIL ALERT"))
            self.radioButton_2.setText(_translate("Dialog", "Bellow Price (x<)"))
            self.pushButton.setText(_translate("Dialog", "START"))
            self.radioButton_3.setText(_translate("Dialog", "get alert once a day"))
            self.radioButton_4.setText(_translate("Dialog", "get alert every time"))
            self.pushButton_2.setText(_translate("Dialog", "EXIT"))
            self.textEdit_4.setHtml(_translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
    "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
    "p, li { white-space: pre-wrap; }\n"
    "</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:16pt; font-weight:400; font-style:normal;\">\n"
    "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
            self.textEdit_5.setHtml(_translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
    "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
    "p, li { white-space: pre-wrap; }\n"
    "</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:16pt; font-weight:400; font-style:normal;\">\n"
    "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
            self.label.setText(_translate("Dialog", "SYMBOL:"))
            self.label_2.setText(_translate("Dialog", "PRICE :"))
            self.label_3.setText(_translate("Dialog", "User of Gmail:"))
            self.label_4.setText(_translate("Dialog", "Password of Gmail:"))
            self.textEdit_2.setHtml(_translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
    "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
    "p, li { white-space: pre-wrap; }\n"
    "</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:18pt; font-weight:400; font-style:normal;\">\n"
    "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
            self.textEdit_3.setHtml(_translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
    "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
    "p, li { white-space: pre-wrap; }\n"
    "</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:18pt; font-weight:400; font-style:normal;\">\n"
    "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
            self.label_5.setText(_translate("Dialog", "ALON STOCK MARKET APP"))
            self.pushButton_3.setText(_translate("Dialog", "STOP"))
           
    
    
    
    
    
    
    
    
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        Dialog = QtWidgets.QDialog()
        ui = Ui_Dialog()
        ui.setupUi(Dialog)
        Dialog.show()
        sys.exit(app.exec_())




More information about the Python-list mailing list