如何只打开一次python软件

问题描述 投票:0回答:1

我已经使用tkinter框架开发了python应用程序,该框架在Windows平台上运行良好,但我遇到的挑战是我可以多次打开此应用程序,如附图所示。

我希望应用程序只打开一次。如果应用程序已打开且用户单击桌面上的图标以再次打开它,则不应打开该应用程序,因为它已被打开。

我希望实现这个功能,因为它适用于Teamviewerpycharm等应用程序。

欢迎您的建议来实现这一目标。

编辑

这是我如何尝试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 windows tkinter
1个回答
0
投票

问题:如何只打开一次python软件

我的评论, 使用psutil查找正在运行的进程并终止。 应该写 使用psutil查找正在运行的进程并退出。

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()
© www.soinside.com 2019 - 2024. All rights reserved.