Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. For loop problem!
Forum Updated to NodeBB v4.3 + New Features

For loop problem!

Scheduled Pinned Locked Moved Solved Qt for Python
15 Posts 2 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M monamour
    12 Feb 2019, 09:58

    Dear All,

    The PROCESS function below, I have "For loop". The first if statement is working fine but the second if statement is not execute at all. It has been changed in so many ways with no success.

    Any ideas,

    Many thanks,

    def Buttons(self):
            self.press_4.clicked.connect(self.Process)
    
    
    def Process(self):
            for proc in psutil.process_iter():
                x=self.lineEdit.text()
                x2= "<font color='#00ff00'> "+x+"</font>"
                if proc.name() not in x:
                    QMessageBox.information(self,"Error",'Blank!')
                    return
    
                if proc.name()  in x:
                    proc.kill()
                    QMessageBox.information(self, "Success", x2 +"has been terminated"
                    return
    
    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 12 Feb 2019, 09:59 last edited by
    #2

    @monamour I only see one loop

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    M 1 Reply Last reply 12 Feb 2019, 10:01
    1
    • J jsulm
      12 Feb 2019, 09:59

      @monamour I only see one loop

      M Offline
      M Offline
      monamour
      wrote on 12 Feb 2019, 10:01 last edited by
      #3

      @jsulm Sorry,I modified the question.

      J 1 Reply Last reply 12 Feb 2019, 10:03
      0
      • M monamour
        12 Feb 2019, 10:01

        @jsulm Sorry,I modified the question.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 12 Feb 2019, 10:03 last edited by jsulm 2 Dec 2019, 10:03
        #4

        @monamour If the first if is true you do return
        Also both if conditions are identical - what's the point?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply 12 Feb 2019, 10:08
        0
        • J jsulm
          12 Feb 2019, 10:03

          @monamour If the first if is true you do return
          Also both if conditions are identical - what's the point?

          M Offline
          M Offline
          monamour
          wrote on 12 Feb 2019, 10:08 last edited by
          #5

          @jsulm "The first if" should execute when the LINEDIT is blank. " The second if" will execute when the LINEDIT is NOT blank.

          J 1 Reply Last reply 12 Feb 2019, 10:09
          0
          • M monamour
            12 Feb 2019, 10:08

            @jsulm "The first if" should execute when the LINEDIT is blank. " The second if" will execute when the LINEDIT is NOT blank.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 12 Feb 2019, 10:09 last edited by jsulm 2 Dec 2019, 10:09
            #6

            @monamour Looks like you edited the code, because before both if were identical.
            Actually you can do it like this:

            if proc.name() not in x:
                            QMessageBox.information(self,"Error",'Blank!')
                            return
            
            else:
                            proc.kill()
                            QMessageBox.information(self, "Success", x2 +"has been terminated"
                            return
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply 12 Feb 2019, 10:12
            0
            • J jsulm
              12 Feb 2019, 10:09

              @monamour Looks like you edited the code, because before both if were identical.
              Actually you can do it like this:

              if proc.name() not in x:
                              QMessageBox.information(self,"Error",'Blank!')
                              return
              
              else:
                              proc.kill()
                              QMessageBox.information(self, "Success", x2 +"has been terminated"
                              return
              
              M Offline
              M Offline
              monamour
              wrote on 12 Feb 2019, 10:12 last edited by monamour 2 Dec 2019, 10:18
              #7

              @jsulm Yes, I edited the code. It is same, not solved.

              J 1 Reply Last reply 12 Feb 2019, 11:14
              0
              • M monamour
                12 Feb 2019, 10:12

                @jsulm Yes, I edited the code. It is same, not solved.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 12 Feb 2019, 11:14 last edited by
                #8

                @monamour Did you check what x is and what proc.name is? Just print out both and see.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                M 1 Reply Last reply 12 Feb 2019, 11:32
                0
                • J jsulm
                  12 Feb 2019, 11:14

                  @monamour Did you check what x is and what proc.name is? Just print out both and see.

                  M Offline
                  M Offline
                  monamour
                  wrote on 12 Feb 2019, 11:32 last edited by
                  #9

                  @jsulm

                  1- proc.name after "IF CONDITIONS"

                  input

                  print(x)
                  print(proc.name())

                  Output

                  notepad.exe
                  notepad.exe

                  2-The full proc.name after "FOR LOOP"

                  input

                  print(proc.name())

                  output

                  System Idle Process
                  System
                  svchost.exe
                  smss.exe
                  csrss.exe
                  MOM.exe
                  wininit.exe
                  csrss.exe
                  services.exe
                  lsass.exe
                  lsm.exe
                  python.exe
                  explorer.exe
                  svchost.exe
                  svchost.exe
                  chrome.exe
                  WSHelper.exe
                  cbInterface.exe
                  taskhost.exe
                  notepad.exe
                  PresentationFontCache.exe
                  SearchFilterHost.exe
                  chrome.exe
                  chrome.exe
                  designer.exe
                  thunderbird.exe
                  fsnotifier64.exe
                  conhost.exe
                  chrome.exe
                  svchost.exe
                  notepad++.exe
                  VSSVC.exe
                  pycharm64.exe
                  chrome.exe
                  chrome.exe
                  SearchProtocolHost.exe
                  conhost.exe
                  chrome.exe

                  Process finished with exit code 0

                  J 1 Reply Last reply 12 Feb 2019, 11:35
                  0
                  • M monamour
                    12 Feb 2019, 11:32

                    @jsulm

                    1- proc.name after "IF CONDITIONS"

                    input

                    print(x)
                    print(proc.name())

                    Output

                    notepad.exe
                    notepad.exe

                    2-The full proc.name after "FOR LOOP"

                    input

                    print(proc.name())

                    output

                    System Idle Process
                    System
                    svchost.exe
                    smss.exe
                    csrss.exe
                    MOM.exe
                    wininit.exe
                    csrss.exe
                    services.exe
                    lsass.exe
                    lsm.exe
                    python.exe
                    explorer.exe
                    svchost.exe
                    svchost.exe
                    chrome.exe
                    WSHelper.exe
                    cbInterface.exe
                    taskhost.exe
                    notepad.exe
                    PresentationFontCache.exe
                    SearchFilterHost.exe
                    chrome.exe
                    chrome.exe
                    designer.exe
                    thunderbird.exe
                    fsnotifier64.exe
                    conhost.exe
                    chrome.exe
                    svchost.exe
                    notepad++.exe
                    VSSVC.exe
                    pycharm64.exe
                    chrome.exe
                    chrome.exe
                    SearchProtocolHost.exe
                    conhost.exe
                    chrome.exe

                    Process finished with exit code 0

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 12 Feb 2019, 11:35 last edited by
                    #10

                    @monamour Maybe both strings use different encodings?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 2 Replies Last reply 12 Feb 2019, 11:37
                    0
                    • J jsulm
                      12 Feb 2019, 11:35

                      @monamour Maybe both strings use different encodings?

                      M Offline
                      M Offline
                      monamour
                      wrote on 12 Feb 2019, 11:37 last edited by
                      #11

                      @jsulm Means? because both of them are strings.

                      J 1 Reply Last reply 12 Feb 2019, 11:45
                      0
                      • J jsulm
                        12 Feb 2019, 11:35

                        @monamour Maybe both strings use different encodings?

                        M Offline
                        M Offline
                        monamour
                        wrote on 12 Feb 2019, 11:39 last edited by
                        #12

                        @jsulm If I make two for loops, it will work. What is the common sense in this ?

                        1 Reply Last reply
                        0
                        • M monamour
                          12 Feb 2019, 11:37

                          @jsulm Means? because both of them are strings.

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 12 Feb 2019, 11:45 last edited by jsulm 2 Dec 2019, 11:47
                          #13

                          @monamour said in For loop problem!:

                          Means? because both of them are strings

                          I mean different string encodings like Latin1, UTF-8, ...
                          Can you try to print out type(x) and type(proc.name())?
                          "If I make two for loops, it will work. What is the common sense in this ?" - I don't know what you mean.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          M 2 Replies Last reply 12 Feb 2019, 11:49
                          0
                          • J jsulm
                            12 Feb 2019, 11:45

                            @monamour said in For loop problem!:

                            Means? because both of them are strings

                            I mean different string encodings like Latin1, UTF-8, ...
                            Can you try to print out type(x) and type(proc.name())?
                            "If I make two for loops, it will work. What is the common sense in this ?" - I don't know what you mean.

                            M Offline
                            M Offline
                            monamour
                            wrote on 12 Feb 2019, 11:49 last edited by monamour 2 Dec 2019, 11:50
                            #14
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • J jsulm
                              12 Feb 2019, 11:45

                              @monamour said in For loop problem!:

                              Means? because both of them are strings

                              I mean different string encodings like Latin1, UTF-8, ...
                              Can you try to print out type(x) and type(proc.name())?
                              "If I make two for loops, it will work. What is the common sense in this ?" - I don't know what you mean.

                              M Offline
                              M Offline
                              monamour
                              wrote on 12 Feb 2019, 11:53 last edited by monamour 2 Dec 2019, 11:54
                              #15

                              @jsulm

                              for proc in psutil.process_iter():
                                          if proc.name()==x:
                                              proc.kill()
                                              QMessageBox.information(self, "Success", x2 +"has been terminated")
                                              return
                              
                               for proc in psutil.process_iter():
                                           if proc.name()!=x:
                                               QMessageBox.information(self, "Error","The process " +x2 + " is not found")
                                               return
                              
                              1 Reply Last reply
                              0

                              11/15

                              12 Feb 2019, 11:37

                              • Login

                              • Login or register to search.
                              11 out of 15
                              • First post
                                11/15
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved