Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

A form with several str, int entries, thoses entries turn into values, values that populate a Method Inside script



  • As we say, all is said in the Title.
    OK, I am fairly new to GYTQ.
    Python, I use it but I am not a born programmer.
    Then, I created a form, you have to input different values as, internet address, int values, etc. Those values are part of a method inside the script.
    On the same window of the form, a start button that launch the method.
    It look like to be something like subprocess or Process command, or something related to Thread.
    I found a few things on the internet but nothing that I understood easily.
    For now I am using pyqt5 but I will switch to pyside2, maybe.
    Link to image

    Here is my code,
    I will continue to edit it, then It can be evolving.

    
    # Notes
    #region
    """
    
            self.textedit = QTextEdit()
            self.textedit.textChanged.connect(self.save_text)
            layout.addWidget(self.textedit)
    
        def save_text(self):
            text = self.textedit.toPlainText()
            with open('mytextfile.txt', 'w') as f:
                f.write(text)
    
    
                def save_text():
            text=textedit.toPlainText()
        with open('mytextfile.txt', 'w') as f:
            f.write(text)
    
    button.clicked.connect(save_text)
    
    https://stackoverflow.com/questions/47560399/run-function-in-the-background-and-update-ui
    
    
    """
    #endregion
    
    #Import
    #region
    from PyQt5.QtWidgets import QApplication, QDialog, QPushButton, QGridLayout, QGroupBox, QVBoxLayout, QLabel, QTextEdit
    import sys
    from PyQt5 import QtGui
    from PyQt5.QtCore import QRect
    from PyQt5 import QtCore
    
    import requests
    from bs4 import BeautifulSoup
    import pandas as pd
    import urllib.request
    #endregion
    
    #Values
    #region
    my_list = []
    substring = "detail"
    
    #endregion
    
    #Window Class
    class Window(QDialog):
        def __init__(self):
            super().__init__()
            self.title = "Find the Data you need"
            self.top = 100
            self.left = 200
            self.width = 800
            self.height =600
            self.iconName = "C:\gitproject\projet3\images\OpenDataChallengeDatasets.jpg"
            self.InitWindow()
            
        def InitWindow(self):
            self.setWindowIcon(QtGui.QIcon(self.iconName))
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
            
            
            
            vbox = QVBoxLayout()
            # Second Big intro Line 
            label2 = QLabel("You will be asked to provide some informations:")
            label2.setFont(QtGui.QFont("Sanserif", 25))
            label2.setStyleSheet('color:red')
            vbox.addWidget(label2)
    
            # First Link to search text
            label = QLabel("1) Please insert the link to search, and Enter")
            label.setFont(QtGui.QFont("Sanserif", 15))
            label.setStyleSheet('color:black')
            vbox.addWidget(label)
    		# Insert html line
            self.lineedit = QTextEdit(self)
            self.lineedit.setFont(QtGui.QFont("Sanserif", 15))
            self.lineedit.textChanged.connect(self.save_text)
            vbox.addWidget(self.lineedit)
    
             # Second magical Link  text
            label6 = QLabel("1) Please insert the magical link, and Enter")
            label6.setFont(QtGui.QFont("Sanserif", 15))
            label6.setStyleSheet('color:black')
            vbox.addWidget(label6)
    		# Insert html line
            self.lineedit5 = QTextEdit(self)
            self.lineedit5.setFont(QtGui.QFont("Sanserif", 15))
            self.lineedit5.textChanged.connect(self.save_text)
            vbox.addWidget(self.lineedit5)
    
            # Insert Text for Iteration
            self.label3 = QLabel("2) The number of iteration, and Enter")
            self.label3.setFont(QtGui.QFont("Sanserif", 15))
            self.label3.setStyleSheet('color:black')
            vbox.addWidget(self.label3)
            # Insert Iteration number line
            self.lineedit2 = QTextEdit(self)
            self.lineedit2.setFont(QtGui.QFont("Sanserif", 15))
            self.lineedit2.textChanged.connect(self.save_text)
            vbox.addWidget(self.lineedit2)
    
            # Insert Text for the word to erase
            self.label4 = QLabel("3) The word to erase, and Enter")
            self.label4.setFont(QtGui.QFont("Sanserif", 15))
            self.label4.setStyleSheet('color:black')
            vbox.addWidget(self.label4)
            # Insert the word to erase line
            self.lineedit3 = QTextEdit(self)
            self.lineedit3.setFont(QtGui.QFont("Sanserif", 15))
            self.lineedit3.textChanged.connect(self.save_text)
            vbox.addWidget(self.lineedit3)
            
            # Insert Text for the CSV name
            self.label5 = QLabel("4) The name of Csv File, and Enter")
            self.label5.setFont(QtGui.QFont("Sanserif", 15))
            self.label5.setStyleSheet('color:black')
            vbox.addWidget(self.label5)
            # Insert the word to erase line
            self.lineedit4 = QTextEdit(self)
            self.lineedit4.setFont(QtGui.QFont("Sanserif", 15))
            self.lineedit4.textChanged.connect(self.save_text)
            vbox.addWidget(self.lineedit4)
    
        
    
            
    
    
    
            # Adding the bellow group box
    
            self.groupBox = QGroupBox("Different actions")
            gridLayout = QGridLayout()
            #First button start
            self.button = QPushButton("Start", self)
            self.button.setWhatsThis("Start the program")
            self.button.setIcon(QtGui.QIcon("C:/gitproject/projet3/images/start.png"))
            self.button.setIconSize(QtCore.QSize(40, 40))
            self.button.setMinimumHeight(40)
            self.button.clicked.connect( self.search1(self, self.text,self.text5, self.text2, self.text3, self.text4 ))
            #  #self.adress1, self.adress2, self.nbrBoucle, self.eraseName, self.nameCsv
            gridLayout.addWidget(self.button, 0,0)
            #Second button Show/Hide the Terminal
            self.button2 = QPushButton("Show/Hide", self)
            self.button2.setWhatsThis("Show/Hide the Terminal to see progression")
            self.button2.setIcon(QtGui.QIcon("C:\gitproject\projet3\images\hide.png"))
            self.button2.setIconSize(QtCore.QSize(40, 40))
            self.button2.setMinimumHeight(40)
            gridLayout.addWidget(self.button2, 0,1)
            
    
            self.groupBox.setLayout(gridLayout)
            vbox.addWidget(self.groupBox)
            self.setLayout(vbox)
            self.show()
            
        def save_text(self):
            if self.lineedit:
                text = self.lineedit.toPlainText()
                print (text)
            if self.lineedit5:
                text5 = self.lineedit5.toPlainText()
                print (text5)
            if self.lineedit:
                text2 = self.lineedit2.toPlainText()
                print (text2)
            if self.lineedit3:
                text3 = self.lineedit3.toPlainText()
                print (text3)
            if self.lineedit4:
                text4 = self.lineedit4.toPlainText()
                print (text4)
            return self.text, self.text2, self.text3, self.text4, self.text5
            
        
        
        
        def search1(self,adress1, adress2, nbrBoucle, eraseName, nameCsv):
            nbrBoucle=int(nbrBoucle)
            for i in range (1,nbrBoucle,1):
                gribou=adress1+str(i)
                result2 = requests.get (gribou)
                a=result2.status_code
                print (a)
                soup = BeautifulSoup(result2.text, 'html.parser')
                for a in soup.find_all('a'):
                    fullstring= a.get('href')
                    if fullstring is None:
                        continue
                    if substring in fullstring:
                        my_list.append(adress2+fullstring)
    
            df = pd.DataFrame(my_list, columns = ['col1'])
            df.sort_values(by=['col1'])
            df.drop_duplicates(keep='first', inplace=True)
            count_row = df.shape[0]  # gives number of row count
            count_col = df.shape[1]
            print (count_row )
            print (count_col )
            print (len(my_list))
            #df.to_csv('HopitalList.csv', index=False)
    
            df2 = pd.DataFrame(columns=['Name', 'Phone','post code','adress','Url','Client Adress'])
            for i in range (1,len(my_list),1):
                url = my_list[i]
                page=urllib.request.urlopen(url)
                soup = BeautifulSoup(page, 'html.parser')
                bloublou4=soup.find_all('td')
                print('                         ')
                bloublou2=soup.find('h1',attrs={"class":"page_title"})
                if bloublou2 is None:
                    bloublou2='None'
                else:
                    bloublou2=bloublou2.text
                    bloublou2=bloublou2.replace(eraseName, '')
                
                if bloublou4[0] is None:
                    bloublou4[0]='None'
                else:
                    bloublou4[0]=bloublou4[0].text
                    bloublou4[0]=bloublou4[0].replace('地図を見る', '')
                    bloublou4a=bloublou4[0][10:]
                    bloublou4b=bloublou4[0][:9]
    
                if bloublou4[1] is None:
                    bloublou4[1]='None'
                else:
                    bloublou4[1]=bloublou4[1].text
                    bloublou4[1]=bloublou4[1][:13]
                
                if len(bloublou4) <31:
                    glagla='None'
                try:
                    glagla=bloublou4[31].text.strip()
            
                except IndexError:
                    glagla='None'
    
                df2.loc[i] = [ bloublou2,bloublou4[1], bloublou4b,bloublou4a, glagla, url]
                
            df2.sort_values(by=['Name'])
            df2.drop_duplicates(keep='first', inplace=True)
            df2.to_csv(nameCsv+".csv", index=False, encoding="utf_8_sig")
    
    
        
            
    
    #Main class
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = Window()
        sys.exit(app.exec())
    

  • Qt Champions 2019

    @tsurubaso It's not clear what your question is.
    What is the other script you want to execute? If it is Python script you can directly use it as you're already using Python. If it is something else you can execute it using https://doc.qt.io/qt-5/qprocess.html (this does not have anything to do with threads).



  • @jsulm
    First a great thanks to answer to my question.

    For the other script,
    it is included inside this script.
    that's the part.
    "def search1"

    and you start it with
    the boutton in the
    "self.button.clicked.connect( self.search1(self, self.text,self.text5, self.text2, self.text3, self.text4 ))"


  • Qt Champions 2019

    @tsurubaso said in A form with several str, int entries, thoses entries turn into values, values that populate a script:

    that's the part.
    "def search1"

    So a method of your class. You should use correct wording, then it is easier to understand.

    This is wrong:

    self.button.clicked.connect( self.search1(self, self.text,self.text5, self.text2, self.text3, self.text4 ))
    

    connect() only connects a signal to a a slot, it does not pass any parameters.
    In this case there is no need to pass all these parameters to search1 if it is a method of the same class: it can access all this stuf via self. If you do not want search1 to be a method then you can add a slot to your class where you collect all the parameters and then call search1 (connect that slot to the button).



  • @jsulm Thanks again,
    Yes, wording is not good, agreed.
    I will change all that.

    "connect() only connects a signal to a a slot, it does not pass any parameters."
    I was willing to launch the script from a bottom, then...obviously, connect is not the good thing to do.

    " it can access all this stuf via self."
    That's good, will try that.
    The simpler the better.

    Thanks for the help!!



  • @tsurubaso
    If all you need to access in the slot is self. variables it can do that there, and you don't need to pass parameters.

    For advanced usage: If you have some local variable in the caller where you do the connect() which you need to pass to the slot, you can do that via Python lambda:

    someVariable = someValue
    self.button.clicked.connect(lambda someVariable: self.search1(someVariable))
    

  • Qt Champions 2019

    @tsurubaso said in A form with several str, int entries, thoses entries turn into values, values that populate a Method Inside script:

    obviously, connect is not the good thing to do

    It is! :-) But you need to do it properly:

    self.button.clicked.connect(self.search1)
    ...
    def search1(self):
        # Here you can access self.text,self.text5, self.text2, self.text3, self.text4
    


  • Thanks you @jsulm and @JonB

    Sorry for the late answer! busy saturday...OK, will work all that and Edit here.

    Thanks again.

    Here is the working script.

    Next objective is to create a button to shut down the interface when finished. DONE

    And also to show the progress of the terminal.

    
    # Notes
    #region
    """
    
    """
    #endregion
    
    #Import
    #region
    from PyQt5.QtWidgets import QApplication, QDialog, QPushButton, QGridLayout, QGroupBox, QVBoxLayout, QLabel, QTextEdit
    import sys
    from PyQt5 import QtGui
    from PyQt5.QtCore import QRect
    from PyQt5 import QtCore
    
    import requests
    from bs4 import BeautifulSoup
    import pandas as pd
    import urllib.request
    #endregion
    
    #Values
    #region
    my_list = []
    substring = "detail"
    
    #endregion
    
    #Window Class
    class Window(QDialog):
        def __init__(self):
            super().__init__()
            self.title = "Find the Data you need"
            self.top = 100
            self.left = 200
            self.width = 800
            self.height =600
            self.iconName = "C:\gitproject\projet3\images\OpenDataChallengeDatasets.jpg"
            self.InitWindow()
            
        def InitWindow(self):
            self.setWindowIcon(QtGui.QIcon(self.iconName))
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
            
            
            
            vbox = QVBoxLayout()
            # Second Big intro Line 
            label2 = QLabel("You will be asked to provide some informations:")
            label2.setFont(QtGui.QFont("Sanserif", 25))
            label2.setStyleSheet('color:red')
            vbox.addWidget(label2)
    
            # First Link to search text
            label = QLabel("1) Please insert the link to search, and Enter")
            label.setFont(QtGui.QFont("Sanserif", 15))
            label.setStyleSheet('color:black')
            vbox.addWidget(label)
    		# Insert html line
            self.lineedit = QTextEdit(self)
            self.lineedit.setFont(QtGui.QFont("Sanserif", 15))
            self.lineedit.textChanged.connect(self.save_text)
            vbox.addWidget(self.lineedit)
    
             # Second magical Link  text
            label6 = QLabel("1) Please insert the magical link, and Enter")
            label6.setFont(QtGui.QFont("Sanserif", 15))
            label6.setStyleSheet('color:black')
            vbox.addWidget(label6)
    		# Insert html line
            self.lineedit5 = QTextEdit(self)
            self.lineedit5.setFont(QtGui.QFont("Sanserif", 15))
            self.lineedit5.textChanged.connect(self.save_text)
            vbox.addWidget(self.lineedit5)
    
            # Insert Text for Iteration
            self.label3 = QLabel("2) The number of iteration, and Enter")
            self.label3.setFont(QtGui.QFont("Sanserif", 15))
            self.label3.setStyleSheet('color:black')
            vbox.addWidget(self.label3)
            # Insert Iteration number line
            self.lineedit2 = QTextEdit(self)
            self.lineedit2.setFont(QtGui.QFont("Sanserif", 15))
            self.lineedit2.textChanged.connect(self.save_text)
            vbox.addWidget(self.lineedit2)
    
            # Insert Text for the word to erase
            self.label4 = QLabel("3) The word to erase, and Enter")
            self.label4.setFont(QtGui.QFont("Sanserif", 15))
            self.label4.setStyleSheet('color:black')
            vbox.addWidget(self.label4)
            # Insert the word to erase line
            self.lineedit3 = QTextEdit(self)
            self.lineedit3.setFont(QtGui.QFont("Sanserif", 15))
            self.lineedit3.textChanged.connect(self.save_text)
            vbox.addWidget(self.lineedit3)
            
            # Insert Text for the CSV name
            self.label5 = QLabel("4) The name of Csv File, and Enter")
            self.label5.setFont(QtGui.QFont("Sanserif", 15))
            self.label5.setStyleSheet('color:black')
            vbox.addWidget(self.label5)
            # Insert the word to erase line
            self.lineedit4 = QTextEdit(self)
            self.lineedit4.setFont(QtGui.QFont("Sanserif", 15))
            self.lineedit4.textChanged.connect(self.save_text)
            vbox.addWidget(self.lineedit4)
    
        
    
            
    
    
    
            # Adding the bellow group box
    
            self.groupBox = QGroupBox("Different actions")
            gridLayout = QGridLayout()
            #First button start
            self.button = QPushButton("Start", self)
            self.button.setWhatsThis("Start the program")
            self.button.setIcon(QtGui.QIcon("C:/gitproject/projet3/images/start.png"))
            self.button.setIconSize(QtCore.QSize(40, 40))
            self.button.setMinimumHeight(40)
            self.button.clicked.connect(self.search1)
            gridLayout.addWidget(self.button, 0,0)
            #Second button Show/Hide the Terminal
            self.button2 = QPushButton("Show/Hide", self)
            self.button2.setWhatsThis("Show/Hide the Terminal to see progression")
            self.button2.setIcon(QtGui.QIcon("C:\gitproject\projet3\images\hide.png"))
            self.button2.setIconSize(QtCore.QSize(40, 40))
            self.button2.setMinimumHeight(40)
            gridLayout.addWidget(self.button2, 0,1)
            #third button Show/Hide the Terminal
            self.button3 = QPushButton("Close", self)
            self.button3.setWhatsThis("This will close the App")
            self.button3.setIcon(QtGui.QIcon("C:\gitproject\projet3\images\close.png"))
            self.button3.setIconSize(QtCore.QSize(40, 40))
            self.button3.setMinimumHeight(40)
            self.button3.clicked.connect(self.close)
            gridLayout.addWidget(self.button3, 1,0)
            #Forth button NO use yet
            self.button4 = QPushButton("NO use yet", self)
            self.button4.setWhatsThis("NO use yet")
            self.button4.setIcon(QtGui.QIcon("C:\gitproject\projet3\images\question.png"))
            self.button4.setIconSize(QtCore.QSize(40, 40))
            self.button4.setMinimumHeight(40)
            gridLayout.addWidget(self.button4, 1,1)
            
    
            self.groupBox.setLayout(gridLayout)
            vbox.addWidget(self.groupBox)
            self.setLayout(vbox)
            self.show()
            
        def save_text(self):
            if self.lineedit:
                self.text = self.lineedit.toPlainText()
                #print (text)
            if self.lineedit5:
                self.text5 = self.lineedit5.toPlainText()
                #print (text5)
            if self.lineedit:
                self.text2 = self.lineedit2.toPlainText()
                #print (text2)
            if self.lineedit3:
                self.text3 = self.lineedit3.toPlainText()
                #print (text3)
            if self.lineedit4:
                self.text4 = self.lineedit4.toPlainText()
                #print (text4)
            return self.text, self.text2, self.text3, self.text4, self.text5
            
        
        
        
        def search1(self):
            
            for i in range (1,int(self.text2),1):
                gribou=self.text+str(i)
                result2 = requests.get (gribou)
                a=result2.status_code
                print (a)
                soup = BeautifulSoup(result2.text, 'html.parser')
                for a in soup.find_all('a'):
                    fullstring= a.get('href')
                    if fullstring is None:
                        continue
                    if substring in fullstring:
                        my_list.append(self.text5+fullstring)
    
            df = pd.DataFrame(my_list, columns = ['col1'])
            df.sort_values(by=['col1'])
            df.drop_duplicates(keep='first', inplace=True)
            count_row = df.shape[0]  # gives number of row count
            count_col = df.shape[1]
            print (count_row )
            print (count_col )
            print (len(my_list))
            #df.to_csv('HopitalList.csv', index=False)
    
            df2 = pd.DataFrame(columns=['Name', 'Phone','post code','adress','Url','Client Adress'])
            for i in range (1,len(my_list),1):
                url = my_list[i]
                page=urllib.request.urlopen(url)
                soup = BeautifulSoup(page, 'html.parser')
                bloublou4=soup.find_all('td')
                print('                         ')
                bloublou2=soup.find('h1',attrs={"class":"page_title"})
                if bloublou2 is None:
                    bloublou2='None'
                else:
                    bloublou2=bloublou2.text
                    bloublou2=bloublou2.replace(self.text3, '')
                
                if bloublou4[0] is None:
                    bloublou4[0]='None'
                else:
                    bloublou4[0]=bloublou4[0].text
                    bloublou4[0]=bloublou4[0].replace('地図を見る', '')
                    bloublou4a=bloublou4[0][10:]
                    bloublou4b=bloublou4[0][:9]
    
                if bloublou4[1] is None:
                    bloublou4[1]='None'
                else:
                    bloublou4[1]=bloublou4[1].text
                    bloublou4[1]=bloublou4[1][:13]
                
                if len(bloublou4) <31:
                    glagla='None'
                try:
                    glagla=bloublou4[31].text.strip()
            
                except IndexError:
                    glagla='None'
    
                df2.loc[i] = [ bloublou2,bloublou4[1], bloublou4b,bloublou4a, glagla, url]
                
            df2.sort_values(by=['Name'])
            df2.drop_duplicates(keep='first', inplace=True)
            df2.to_csv(self.text4+".csv", index=False, encoding="utf_8_sig")
    
    
        
            
    
    #Main class
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = Window()
        sys.exit(app.exec())
    

Log in to reply