zoukankan      html  css  js  c++  java
  • Python程序互斥体

    Python程序互斥体

      有时候我们需要程序只运行一个实例,在windows平台下我们可以很简单的用mutex实现这个目的。
      在开始时,程序创建了一个命名的mutex,这个mutex可以被其他进程检测到。 这样如果程序已经启动,再次运行时的进程就可以检测到程序已运行,从而不会继续运行。

    from tkinter import *
    import win32event, pywintypes, win32api
    from winerror import ERROR_ALREADY_EXISTS
    class MyFrm(Frame):
        def __init__(self, master):
            self.root=master
            self.screen_width = self.root.winfo_screenwidth()#获得屏幕宽度
            self.screen_height = self.root.winfo_screenheight()  #获得屏幕高度
            #self.root.resizable(False, False)#让高宽都固定
            self.root.update_idletasks()#刷新GUI
            self.root.withdraw() #暂时不显示窗口来移动位置
            self.root.geometry('%dx%d+%d+%d' % (self.root.winfo_width(), self.root.winfo_height() ,(self.screen_width - self.root.winfo_width()) / 2,(self.screen_height - self.root.winfo_height()) / 2))  # center window on desktop
            self.root.deiconify()
            Label(self.root,text='程序运行中...').pack(fill=BOTH,expand=YES)
    
    if __name__=='__main__':
        mutexname = "DEMO"#互斥体命名
        mutex = win32event.CreateMutex(None, FALSE, mutexname)
        if (win32api.GetLastError() == ERROR_ALREADY_EXISTS):
            print('程序已启动')
            exit(0)
        root=Tk()
        MyFrm(root)
        root.mainloop()
  • 相关阅读:
    13、java中8中基本类型
    12、static final
    11、final详解
    10、java初始化顺序
    9、java中static详解
    9、接口和抽象类
    8、java内部类
    7、手工编译和运行时注意事项
    推荐10 款 SVG 动画的 JavaScript 库
    让优秀的文章脱颖而出---极客头条使用体验
  • 原文地址:https://www.cnblogs.com/d0main/p/7613302.html
Copyright © 2011-2022 走看看