Python: how to prevent an application from opening if it is already running

I am developing Windows software for Windows 7 encoded in Python with Wxpython for the GUI. I do not want to open my software if it is already open. I want this function, if the user opens this software, a window appears on the Windows screen with a message that "your application is already running."

Help Plz. Thanks in advance...

+6
source share
1 answer

There already exists a wxPython tool that implements a wanted logic called wx.SingleInstanceChecker . Here is a sample code (shamelessly borrowed from the wxPython wiki ):

 import wx class SingleAppFrame(wx.Frame): def __init__(self, parent, title): wx.Frame.__init__(self, parent, title=title, size=(300, 300)) self.Centre() class SingleApp(wx.App): def OnInit(self): self.name = "SingleApp-%s" % wx.GetUserId() self.instance = wx.SingleInstanceChecker(self.name) if self.instance.IsAnotherRunning(): wx.MessageBox("Another instance is running", "ERROR") return False frame = SingleAppFrame(None, "SingleApp") frame.Show() return True app = SingleApp(redirect=False) app.MainLoop() 

This cannon example (for good luck) does what you requested.

+9
source

Source: https://habr.com/ru/post/923275/


All Articles