鱼C论坛

 找回密码
 立即注册
查看: 5332|回复: 2

[已解决]python 如何实现"按指定键暂停(挂起)程序"

[复制链接]
发表于 2020-9-18 22:10:01 | 显示全部楼层 |阅读模式
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
最佳答案
2020-9-18 22:10:02
看看是不是你要的效果
  1. from pynput.keyboard import Listener
  2. import time
  3. from tkinter import *
  4. import os
  5. from multiprocessing import Process
  6. import psutil


  7. # 设置进程1,实现目标:按键监听,当按下“d”键时,进程2能挂起(暂停运行),并弹窗提示"继续程序"和"终止程序"按钮。
  8. # 当按下"继续程序"按钮时,进程2能接下去运行,当按下"终止程序"时,进程2能结束运行。
  9. def work1(pid, mainpid):
  10.     # 按键监听,当按下“d"键时弹窗
  11.     def on_press(key):
  12.         all_key = []
  13.         all_key.append(str(key))
  14.         if "'d'" in all_key:
  15.             pause = psutil.Process(pid)  # 传入子进程的pid
  16.             pause.suspend()  # 暂停子进程
  17.             print(f'{pid}子进程已暂停。。。。')

  18.     def on_release(key):
  19.         all_key = []
  20.         all_key.append(str(key))
  21.         if "'d'" in all_key:
  22.             def zzcx():
  23.                 pipause = psutil.Process(mainpid)  # 传入主进程的pid
  24.                 pipause.kill()
  25.                 print(f'{mainpid}主进程已结束。。。。')
  26.                 tanchuan_zt.destroy()

  27.             def jxcx():
  28.                 pause = psutil.Process(pid)  # 传入子进程的pid
  29.                 pause.resume()  # 暂停子进程
  30.                 print(f'{pid}子进程已继续。。。。')

  31.             # 设置弹窗
  32.             tanchuan_zt = Tk()
  33.             tanchuan_zt.title("终止/暂停程序")
  34.             wb_zt = Label(tanchuan_zt, text="您按下了”D(d)“键,此为暂停程序键,\n请问是否继续", compound=CENTER,
  35.                           font=("楷体", 20), fg="black")
  36.             wb_zt.pack(pady=20)
  37.             xx1_button = Button(tanchuan_zt, text="终止程序", command=zzcx)
  38.             xx1_button.pack(side="left", padx=120)
  39.             xx2_button = Button(tanchuan_zt, text="继续程序", command=jxcx)
  40.             xx2_button.pack(side="right", padx=120)

  41.             # 弹窗位置大小设置
  42.             width = 700
  43.             height = 200
  44.             screenwidth = tanchuan_zt.winfo_screenwidth()
  45.             screenheight = tanchuan_zt.winfo_screenheight()
  46.             alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
  47.             tanchuan_zt.geometry(alignstr)
  48.             tanchuan_zt.mainloop()

  49.             all_key.clear()  # 对列表进行清空

  50.         # if key == Key.esc:  # 停止监听
  51.         #     return Falseurn False

  52.     def start_listen():  # 设置监听
  53.         with Listener(on_press=on_press, on_release=on_release) as listener:
  54.             listener.join()

  55.     start_listen()  # 启动监听


  56. # 设置进程2,一个普通的打印数字程序
  57. def work2():
  58.     for i in range(1, 1000):
  59.         print(i)
  60.         time.sleep(1)


  61. if __name__ == '__main__':
  62.     mainpid = os.getpid()  # 获取主进程pid
  63.     p2 = Process(target=work2)
  64.     p2.start()
  65.     pid = p2.pid  # 获取子进程pid
  66.     p1 = Process(target=work1, args=(pid, mainpid))
  67.     p1.start()
复制代码

最佳答案

查看完整内容

看看是不是你要的效果
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-18 22:10:02 | 显示全部楼层    本楼为最佳答案   
看看是不是你要的效果
  1. from pynput.keyboard import Listener
  2. import time
  3. from tkinter import *
  4. import os
  5. from multiprocessing import Process
  6. import psutil


  7. # 设置进程1,实现目标:按键监听,当按下“d”键时,进程2能挂起(暂停运行),并弹窗提示"继续程序"和"终止程序"按钮。
  8. # 当按下"继续程序"按钮时,进程2能接下去运行,当按下"终止程序"时,进程2能结束运行。
  9. def work1(pid, mainpid):
  10.     # 按键监听,当按下“d"键时弹窗
  11.     def on_press(key):
  12.         all_key = []
  13.         all_key.append(str(key))
  14.         if "'d'" in all_key:
  15.             pause = psutil.Process(pid)  # 传入子进程的pid
  16.             pause.suspend()  # 暂停子进程
  17.             print(f'{pid}子进程已暂停。。。。')

  18.     def on_release(key):
  19.         all_key = []
  20.         all_key.append(str(key))
  21.         if "'d'" in all_key:
  22.             def zzcx():
  23.                 pipause = psutil.Process(mainpid)  # 传入主进程的pid
  24.                 pipause.kill()
  25.                 print(f'{mainpid}主进程已结束。。。。')
  26.                 tanchuan_zt.destroy()

  27.             def jxcx():
  28.                 pause = psutil.Process(pid)  # 传入子进程的pid
  29.                 pause.resume()  # 暂停子进程
  30.                 print(f'{pid}子进程已继续。。。。')

  31.             # 设置弹窗
  32.             tanchuan_zt = Tk()
  33.             tanchuan_zt.title("终止/暂停程序")
  34.             wb_zt = Label(tanchuan_zt, text="您按下了”D(d)“键,此为暂停程序键,\n请问是否继续", compound=CENTER,
  35.                           font=("楷体", 20), fg="black")
  36.             wb_zt.pack(pady=20)
  37.             xx1_button = Button(tanchuan_zt, text="终止程序", command=zzcx)
  38.             xx1_button.pack(side="left", padx=120)
  39.             xx2_button = Button(tanchuan_zt, text="继续程序", command=jxcx)
  40.             xx2_button.pack(side="right", padx=120)

  41.             # 弹窗位置大小设置
  42.             width = 700
  43.             height = 200
  44.             screenwidth = tanchuan_zt.winfo_screenwidth()
  45.             screenheight = tanchuan_zt.winfo_screenheight()
  46.             alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
  47.             tanchuan_zt.geometry(alignstr)
  48.             tanchuan_zt.mainloop()

  49.             all_key.clear()  # 对列表进行清空

  50.         # if key == Key.esc:  # 停止监听
  51.         #     return Falseurn False

  52.     def start_listen():  # 设置监听
  53.         with Listener(on_press=on_press, on_release=on_release) as listener:
  54.             listener.join()

  55.     start_listen()  # 启动监听


  56. # 设置进程2,一个普通的打印数字程序
  57. def work2():
  58.     for i in range(1, 1000):
  59.         print(i)
  60.         time.sleep(1)


  61. if __name__ == '__main__':
  62.     mainpid = os.getpid()  # 获取主进程pid
  63.     p2 = Process(target=work2)
  64.     p2.start()
  65.     pid = p2.pid  # 获取子进程pid
  66.     p1 = Process(target=work1, args=(pid, mainpid))
  67.     p1.start()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-9-20 17:15:18 | 显示全部楼层
疾风怪盗 发表于 2020-9-20 01:43
看看是不是你要的效果

完美运行,感谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-13 02:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表