How to kill a pyqt based GUI on windows platform
-
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 = Nonedef 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
-
@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.
-
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 = Nonedef 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
-
Please suggest better way that I test invoking and quit of GUI in behave test case