希望するロジックを実装している既存のwxPythonの機能があります(wx.SingleInstanceChecker
)。ここだとコードの例(臆面もなく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()
(運の問題のために)このcannonical例は、あなたが求めてきましたexatlyものを作ります。
ちょっと好奇心... Python 3とwxPythonをどのように組み合わせるのですか?私が知る限り、Python 3.x用のwxPythonはありません。おそらく関連するタグを削除するべきです。 – pepr