python打包成exe闪退_脚本程序打包后,黑框一闪而过,程序不能运行

源程序的链接是我另一个求助帖:https://fishc.com.cn/forum.php?m ... p;page=1#pid4988050

源程序已经完美解决,能够在pycharm实现了,再次感谢大佬@疾风怪盗

但是目前打包却出现了错误,就连 打包前脱离pycharm,双击.py文件先运行下程序都不行,也是出现黑框,ImportError

今天百度了下,大概可能是:

1. 不能用 from xx import xx 这种导入方式

2. Process 多进程打包还涉及一个什么GIL锁

总之还没搞懂

aru-1x-1_005.png

打包代码(我对代码做了点小小的改动):

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()

os._exit(0)

def jxcx():

pause = psutil.Process(pid) # 传入子进程的pid

pause.resume() # 暂停子进程

#print(f'{pid}子进程已继续。。。。')

tanchuan_zt.destroy()

# 设置弹窗

tanchuan_zt = Tk()

tanchuan_zt.wm_attributes('-topmost', 1) # 弹窗置顶

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(mainpid):

for i in range(1, 10):

print(i)

time.sleep(1)

pipause2 = psutil.Process(mainpid) # 传入主进程的pid

pipause2.kill()

os._exit(0)

if __name__ == '__main__':

mainpid = os.getpid() # 获取主进程pid

p2 = Process(target=work2,args=(mainpid,))

p2.start()

pid = p2.pid # 获取子进程pid

p1 = Process(target=work1, args=(pid, mainpid))

p1.start()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值