|
20鱼币
我想实现程序可以按指定键(例如"d"键)后暂停程序
于是自己百度弄了两个进程,
进程1:实现键盘监听,当按下d键时,暂停进程2
进程2:正常需要运行的程序
但是自己写的程序运行时,进程2还是在运行,请问各位大佬怎么改进?
或者有更好的方法(比如不用开2个进程就能实现这个目标的方法)
新人,自己研究了两个晚上,实在搞不定
自己写的代码如下:
from pynput.keyboard import Listener
import time
from tkinter import *
import os
from multiprocessing import Process
import psutil
#设置进程1,实现目标:按键监听,当按下“d”键时,进程2能挂起(暂停运行),并弹窗提示"继续程序"和"终止程序"按钮。
# 当按下"继续程序"按钮时,进程2能接下去运行,当按下"终止程序"时,进程2能结束运行。
def work1():
def zzcx():
os._exit(0) #为之后tkinter中“终止程序”按钮设置调用命令, 然而这个终止只能终止进程1...
#按键监听,当按下“d"键时弹窗
def on_press(key):
pass
def on_release(key):
all_key = []
all_key.append(str(key))
if "'d'" in all_key:
#下面这两句有毒,进程2还没开始,没有pid,并不能实现挂起,先注释掉
# pause = psutil.Process(pid) #传入子进程的pid
# pause.suspend() # 暂停子进程
#设置弹窗
tanchuan_zt = Tk()
tanchuan_zt.title("终止/暂停程序")
wb_zt = Label(tanchuan_zt, text="您按下了”D(d)“键,此为暂停程序键,\n请问是否继续", compound=CENTER,
font=("楷体", 20), fg="black")
wb_zt.pack(pady=20)
xx1_button = Button(tanchuan_zt,text="终止程序",command=zzcx)
xx1_button.pack(side="left",padx = 120)
xx2_button = Button(tanchuan_zt,text="继续程序",command=tanchuan_zt.destroy)
xx2_button.pack(side="right",padx = 120)
#弹窗位置大小设置
width = 700
height = 200
screenwidth = tanchuan_zt.winfo_screenwidth()
screenheight = tanchuan_zt.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
tanchuan_zt.geometry(alignstr)
tanchuan_zt.mainloop()
all_key.clear() #对列表进行清空
# if key == Key.esc: # 停止监听
# return Falseurn False
def start_listen():
with Listener(on_press=None, on_release=on_release) as listener:
listener.join()
start_listen()
#设置进程2,一个普通的打印数字程序
def work2():
for i in range(1,1000):
print(i)
time.sleep(1)
if __name__ == '__main__':
p1 = Process(target=work1)
p2 = Process(target=work2)
p1.start()
p2.start()
pid = p2.pid # 获取子进程的pid
看看是不是你要的效果 from pynput.keyboard import Listener
import time
from tkinter import *
import os
from multiprocessing import Process
import psutil
# 设置进程1,实现目标:按键监听,当按下“d”键时,进程2能挂起(暂停运行),并弹窗提示"继续程序"和"终止程序"按钮。
# 当按下"继续程序"按钮时,进程2能接下去运行,当按下"终止程序"时,进程2能结束运行。
def work1(pid, mainpid):
# 按键监听,当按下“d"键时弹窗
def on_press(key):
all_key = []
all_key.append(str(key))
if "'d'" in all_key:
pause = psutil.Process(pid) # 传入子进程的pid
pause.suspend() # 暂停子进程
print(f'{pid}子进程已暂停。。。。')
def on_release(key):
all_key = []
all_key.append(str(key))
if "'d'" in all_key:
def zzcx():
pipause = psutil.Process(mainpid) # 传入主进程的pid
pipause.kill()
print(f'{mainpid}主进程已结束。。。。')
tanchuan_zt.destroy()
def jxcx():
pause = psutil.Process(pid) # 传入子进程的pid
pause.resume() # 暂停子进程
print(f'{pid}子进程已继续。。。。')
# 设置弹窗
tanchuan_zt = Tk()
tanchuan_zt.title("终止/暂停程序")
wb_zt = Label(tanchuan_zt, text="您按下了”D(d)“键,此为暂停程序键,\n请问是否继续", compound=CENTER,
font=("楷体", 20), fg="black")
wb_zt.pack(pady=20)
xx1_button = Button(tanchuan_zt, text="终止程序", command=zzcx)
xx1_button.pack(side="left", padx=120)
xx2_button = Button(tanchuan_zt, text="继续程序", command=jxcx)
xx2_button.pack(side="right", padx=120)
# 弹窗位置大小设置
width = 700
height = 200
screenwidth = tanchuan_zt.winfo_screenwidth()
screenheight = tanchuan_zt.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
tanchuan_zt.geometry(alignstr)
tanchuan_zt.mainloop()
all_key.clear() # 对列表进行清空
# if key == Key.esc: # 停止监听
# return Falseurn False
def start_listen(): # 设置监听
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
start_listen() # 启动监听
# 设置进程2,一个普通的打印数字程序
def work2():
for i in range(1, 1000):
print(i)
time.sleep(1)
if __name__ == '__main__':
mainpid = os.getpid() # 获取主进程pid
p2 = Process(target=work2)
p2.start()
pid = p2.pid # 获取子进程pid
p1 = Process(target=work1, args=(pid, mainpid))
p1.start()
|
|