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. How to kill a pyqt based GUI on windows platform
Forum Updated to NodeBB v4.3 + New Features

How to kill a pyqt based GUI on windows platform

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 4 Posters 1.5k Views 2 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on 1 Mar 2020, 06:07 last edited by
    #1

    class start_process_and_kill():
    # behave testcase code to check process gui invoke on windows/linux platform
    def init(self):
    self.cmd = None
    self.process_output = None

    def kill_process_in_linux(self):
        cmd = "pkill -f " + self.cmd
        time.sleep(5)
        os.system(cmd)
    
    def kill_process_in_windows(self):
        
        time.sleep(5)
        print("\n IN kill_process_in_windows") 		
    

    processes = [process.info for process in psutil.process_iter(attrs=['pid', 'name']) if 'python' in process.info['name']]
    max_time = "00:00:00"
    process_pid = None
    process_info_dict = {}
    for process_info in processes:

            process_detail_info = psutil.Process(process_info["pid"])
            process_creation_time = datetime.datetime.fromtimestamp(process_detail_info.create_time()).strftime("%H:%M:%S")
            process_info_dict[process_creation_time] = process_info
            print(process_info_dict)
    	
        max_time = max(process_info_dict.keys())
        required_pid = process_info_dict[max_time]["pid"]
        strin1 = "taskkill /F /PID {}".format(required_pid)
        #os.system('taskkill /F /PID' + required_pid)
        print("11")
    
    def call_process(self):
    self.process_output = subprocess.check_output(self.cmd, shell=True, stderr=subprocess.STDOUT)
    
    def sp_call(self,cmdline,shell=False,module=False):
        self.cmd = cmdline
        process_kill = None
        if platform.system().find("Linux") >= 0:
            process_kill = threading.Thread(target=self.kill_process_in_linux)
        elif platform.system().find("Windows") >= 0:
            process_kill = threading.Thread(target=self.kill_process_in_windows)
        process_start = threading.Thread(target=self.call_process)
    
        process_kill.start()
        process_start.start()
    
        process_kill.join()
        process_start.join()
    
        return self.process_output
    
    J 1 Reply Last reply 1 Mar 2020, 08:15
    0
    • Q Qt Enthusiast
      1 Mar 2020, 06:07

      class start_process_and_kill():
      # behave testcase code to check process gui invoke on windows/linux platform
      def init(self):
      self.cmd = None
      self.process_output = None

      def kill_process_in_linux(self):
          cmd = "pkill -f " + self.cmd
          time.sleep(5)
          os.system(cmd)
      
      def kill_process_in_windows(self):
          
          time.sleep(5)
          print("\n IN kill_process_in_windows") 		
      

      processes = [process.info for process in psutil.process_iter(attrs=['pid', 'name']) if 'python' in process.info['name']]
      max_time = "00:00:00"
      process_pid = None
      process_info_dict = {}
      for process_info in processes:

              process_detail_info = psutil.Process(process_info["pid"])
              process_creation_time = datetime.datetime.fromtimestamp(process_detail_info.create_time()).strftime("%H:%M:%S")
              process_info_dict[process_creation_time] = process_info
              print(process_info_dict)
      	
          max_time = max(process_info_dict.keys())
          required_pid = process_info_dict[max_time]["pid"]
          strin1 = "taskkill /F /PID {}".format(required_pid)
          #os.system('taskkill /F /PID' + required_pid)
          print("11")
      
      def call_process(self):
      self.process_output = subprocess.check_output(self.cmd, shell=True, stderr=subprocess.STDOUT)
      
      def sp_call(self,cmdline,shell=False,module=False):
          self.cmd = cmdline
          process_kill = None
          if platform.system().find("Linux") >= 0:
              process_kill = threading.Thread(target=self.kill_process_in_linux)
          elif platform.system().find("Windows") >= 0:
              process_kill = threading.Thread(target=self.kill_process_in_windows)
          process_start = threading.Thread(target=self.call_process)
      
          process_kill.start()
          process_start.start()
      
          process_kill.join()
          process_start.join()
      
          return self.process_output
      
      J Offline
      J Offline
      JonB
      wrote on 1 Mar 2020, 08:15 last edited by
      #2

      @Qt-Enthusiast
      Is there a question here?

      Since you post quite a lot it would be really nice, at least for me, if you learnt to mark your code as code here.

      Killing a Python PyQt GUI process should be no different than killing any other process.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 1 Mar 2020, 08:20 last edited by
        #3

        Hi,

        Beside @JonB's good point, why not just use QApplication.quit ?
        What is your exact use case ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • Q Offline
          Q Offline
          Qt Enthusiast
          wrote on 1 Mar 2020, 12:52 last edited by
          #4

          I have to write a behave test case

          where I have test invoking of the pyqt based gui and stop the gui .

          @given(u'an GUI invoke command')
          def step_impl(context):
          print("\n 1111")
          file1 = open("myfile.txt","a")
          file1.write("\n1111")
          sys.stdout.flush()
          context.command = "guiexec &"
          file1.write("\n2222")
          file1.close()
          print("\n 3333")
          sys.stdout.flush()

          @when(u'the GUI invoke command is run')
          def step_impl(context):
          context.co = start_process_and_kill()
          context.result = context.co.sp_call(context.command)

          class start_process_and_kill():
          # behave testcase code to check process gui invoke on windows/linux platform
          def init(self):
          self.cmd = None
          self.process_output = None

          def start_proces(self,cmd):
               subprocess.Popen(cmd) 	
          
          def kill_process_in_linux(self):
              cmd = "pkill -f " + self.cmd
              time.sleep(5)
              os.system(cmd)
          
          def kill_process_in_windows(self):
              
              time.sleep(5)
              file1=  open("myfile.txt","a")
              file1.write("\nwindows start")
              file1.close()
              
              print("\n IN kill_process_in_windows") 		
              processes = [process.info for process in psutil.process_iter(attrs=['pid', 'name']) if 'python' in process.info['name']]
              max_time = "00:00:00"
              process_pid = None
              process_info_dict = {}
              for process_info in processes:
          	
                  process_detail_info = psutil.Process(process_info["pid"])
                  process_creation_time = datetime.datetime.fromtimestamp(process_detail_info.create_time()).strftime("%H:%M:%S")
                  process_info_dict[process_creation_time] = process_info
                  print(process_info_dict)
          	
              #max_time = max(process_info_dict.keys())
              required_pid = process_info_dict[max_time]["pid"]
              strin1 = "taskkill /F /PID {}".format(required_pid)
              #os.system('taskkill /F /PID' + required_pid)
          	
              keylist = process_info_dict.keys()
              file1=  open("myfile.txt","a")
              file1.write("\nwindows kill111")
              file1.close()
              keylist.sort() 
              key2 = keylist[-2]
              pid = process_info_dict[key2]['pid']
              pidStr = repr(pid)
              str = 'taskkill /F /PID ' + pidStr
              print(str)
              os.system(str)				
              print("11")
          
          def call_process(self):
          self.process_output = subprocess.check_output(self.cmd, shell=True, stderr=subprocess.STDOUT)
          
          def sp_call(self,cmdline,shell=False,module=False):
              self.cmd = cmdline
              process_kill = None
              if platform.system().find("Linux") >= 0:
                  process_kill = threading.Thread(target=self.kill_process_in_linux)
              elif platform.system().find("Windows") >= 0:
                  print("1112")      
                  process_kill = threading.Thread(target=self.kill_process_in_windows)
                  file1 = open("myfile.txt","a")
                  file1.write("\nwindows kill")
                  file1.close() 			
          
          		#subprocess.Popen(self.kill_process_in_windows)
              process_start = threading.Thread(target=self.call_process)
          
              process_kill.start()
              process_start.start()
          
              process_kill.join()
              process_start.join()
          	
              file1 = open("myfile.txt","a")
              file1.write("\nwindows kill done")
              file1.close() 			
          
              return self.process_output
          
          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on 1 Mar 2020, 12:52 last edited by
            #5

            Please suggest better way that I test invoking and quit of GUI in behave test case

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 1 Mar 2020, 19:48 last edited by
              #6

              Hi,

              Use something designed for Qt testing like pytest-qt.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2

              6/6

              1 Mar 2020, 19:48

              • Login

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