I am making a wxpython app that needs to have internet connection as a must . So , i need to check if connection is present , and if not , then do an exit . So my code looks like this .
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title,
size=(700,650))
self.InitUI()
self.Centre()
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
#--------------------------------------------------------------------------------
favicon = wx.Icon(ICON_PATH, wx.BITMAP_TYPE_ICO)
wx.Frame.SetIcon(self, favicon)
self.Show()
def InitUI(self):
#checkConnection()
checkInstances()
panel = wx.Panel(self)
font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
font.SetPointSize(9)
font1 = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
font1.SetPointSize(10)
#Layout
vbox = wx.BoxSizer(wx.VERTICAL)
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
st1 = wx.StaticText(panel, label='Select Slot :')
st1.SetFont(font1)
hbox1.Add(st1, flag=wx.ALL, border=8)
try:
handler = urllib.urlopen("http://%s/api/get-slots/?api_key=%s"%(BASE_DOMAIN, API_KEY))
except:
wx.MessageBox("Error communicating with the server.", 'WARNING',wx.OK |wx.ICON_INFORMATION)
handler = None
sys.exit(0)
So i want to check for that link above (some link) , and if it fails , i display a message box , and then I exit . When i run this (with internet off) , the app does shows the message , and after clicking OK , another traceback box opens with a print
SystemExit:0
It does not actually exit . Tried using self.Close also . That does’nt work too .
Thought of making a checkconnection function as soon as we run the app .
def checkConnection():
try:
test = urllib.urlopen("http://www.google.com")
except:
sys.exit(0)
Placed it at
def InitUI(self):
checkConnection()
.........
.........
This also did the same thing . So is there some way to avoid the printing
and ACTUALLY EXIT?
os._exit worked but I just thought that sys.exit should have worked too .
So any hints guys?
Instead of
sys.exit(oros._exit) useos._exithalts the process abruptly without calling any cleanup handlers and should generally not be used (except to halt child processes after a fork).sys.exitraises a SystemExit exception.wxpythonmust be catching this exception and choosing to print the error message without quitting the app.self.Destroy()should quit the app.