简体   繁体   English

Python:如果应用程序已在运行,如何禁止打开它

[英]Python:How to prohibit opening of an application if it is already running

I am designing a windows utility software for Windows 7 coded in Python with Wxpython for GUI works.I dont want to open my software if it is already opened. 我设计Python编写与wxPython的 GUI的 works.I不用想,如果它已经打开了,打开我的软件适用于Windows 7在Windows实用软件。 I want a function like this if user opens that software a message box is to be displayed on windows screen showing that "Your application is already running". 我希望这样的功能,如果用户打开该软件,将在Windows屏幕上显示一个消息框,显示“您的应用程序已在运行”。

Plz help. Plz的帮助。 Thanks in advance... 提前致谢...

There's already existing wxPython facility that implements wanted logic, called wx.SingleInstanceChecker . 现有的wxPython工具已经实现了所需的逻辑,称为wx.SingleInstanceChecker Here's and example of code (shamelessly borrowed from wxPython wiki ): 这是代码的例子(从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 cannonical example (for a matter of luck) makes exatly what you've asked. 这个非常典型的例子(为了运气)可以很好地满足你的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM