python - 如何只打开一次python软件
问题描述
我已经开发了带有tkinter
在 Windows 平台上运行良好的框架的 python 应用程序,但我面临的挑战是我可以多次打开这个应用程序,如附图所示。
我希望应用程序只打开一次。如果应用程序已打开并且用户单击桌面上的图标以再次打开它,则它不应该打开,因为它已经被打开了。
我想实现此功能,因为它适用于 等应用Teamviewer
程序pycharm
。
欢迎您提出建议以实现这一目标。
编辑
这是我尝试terminate
的方式,但是在将其添加到代码中之后,可执行文件不再运行;
import psutil
import tkinter as tk
root = tk.Tk()
root.geometry("400x400")
b = tk.Button(root, text="hello world button", command=None)
b.place(x=200, y=100)
PROCNAME = "myapp.exe"
for proc in psutil.process_iter():
# check whether the process name matches
if proc.name() == PROCNAME:
print("Running, exit(1).")
exit(1)
else:
print("not running, continue to startup.")
root.mainloop()
root.mainloop()
解决方案
问题:如何只打开一次python软件
我的评论,
使用 psutil 查找正在运行的进程并终止。
应该写成
Find running processes using psutil and exit。
if __name__ == "__main__":
PROCNAME = "myapp.exe"
for proc in psutil.process_iter():
# check whether the process name matches
if proc.name() == PROCNAME:
print("Running, exit(1).")
exit(1)
print("not running, continue to startup.")
App().mainloop()
推荐阅读
- python - Keras / NN - 处理 NaN,缺少输入
- typo3 - 如何使用作曲家更新 TYPO3 扩展?
- c# - 当在asp.net mvc中首先无法识别控制器时,如何路由到控制器?
- python - 从两个参差不齐的张量中选择随机条目
- ruby - 具有特定顺序的狮身人面像搜索
- python - Python C API:PyDateTime_FromTimestamp 导致分段错误
- powerbi - 如何在 PowerBI 中开发反向切片器?
- python - 将复选框数组从 js 发送到 django 视图
- javascript - muuri 拖放在 Safari Mac 上不起作用
- machine-learning - 大图像上的目标检测训练