首页 > 解决方案 > 如何只打开一次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()

标签: pythonwindowstkinter

解决方案


问题:如何只打开一次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()

推荐阅读