import wx
import serial
import time
import os
import win32com.client
import win32net
import subprocess
import sys
ID_EXIT = 102
class Samuel(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Notification System', size=(350,250))
panel = wx.Panel(self)
font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
font.SetPointSize(0)
vbox = wx.BoxSizer(wx.VERTICAL)
status=self.CreateStatusBar()
self.SetStatusText("Ping N SmS")
#Set text box for Host
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
st1 = wx.StaticText(panel, wx.ID_ANY, label='Host ')
st1.SetFont(font)
hbox1.Add(st1, flag=wx.RIGHT, border=20)
self.tc = wx.TextCtrl(panel, wx.ID_ANY)
hbox1.Add(self.tc, proportion=1)
vbox.Add(hbox1, flag=wx.LEFT|wx.EXPAND|wx.RIGHT|wx.TOP, border=10)
vbox.Add((-1, 10))
#Set text box for #number
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
st2 = wx.StaticText(panel, wx.ID_ANY, label='Phone Number')
st2.SetFont(font)
hbox2.Add(st2,flag=wx.RIGHT, border=10)
self.tc1 = wx.TextCtrl(panel, wx.ID_ANY)
hbox2.Add(self.tc1, proportion=2)
vbox.Add(hbox2, flag=wx.LEFT | wx.TOP, border=10)
vbox.Add((-1, 10))
#set Text for Notification Type
hbox3 = wx.BoxSizer(wx.HORIZONTAL)
st3 = wx.StaticText(panel, label='Notification Type')
st3.SetFont(font)
hbox3.Add(st3,flag=wx.RIGHT, border=10)
vbox.Add(hbox3, flag=wx.LEFT | wx.TOP, border=10)
#set list box for Notification Type
mylist=['SMS', 'CALL']
c***=wx.ListBox(panel, -1, (130,90), (50,30), mylist, wx.LB_SINGLE)
c***.SetSelection(1)
# Run buttons to notification
hbox5 = wx.BoxSizer(wx.HORIZONTAL)
btn1 = wx.Button(panel, wx.ID_ANY, label='Run', size=(70, 30))
self.Bind(wx.EVT_BUTTON, self.OnSubmit, btn1)
btn1.SetDefault()
btn1.SetSize(btn1.GetBestSize())
hbox5.Add(btn1)
#close button on Interface
box = wx.BoxSizer(wx.HORIZONTAL)
m_close = wx.Button(panel, wx.ID_CLOSE, label='Close', size=(70, 30))
m_close.Bind(wx.EVT_BUTTON, self.OnClose)
hbox5.Add(m_close, flag=wx.LEFT|wx.BOTTOM, border=5)
vbox.Add(hbox5, flag=wx.ALIGN_RIGHT|wx.RIGHT, border=10)
panel.SetSizer(vbox)
#menu
menubar=wx.MenuBar()
first=wx.Menu()
first.Append(ID_EXIT,"Exit", "This will close the window")
menubar.Append(first,"File")
self.SetMenuBar(menubar)
self.Bind(wx.EVT_MENU, self.onMenuExit, id=ID_EXIT)
self.Bind(wx.EVT_CLOSE, self.onCloseWindow)
self.Show(True)
#submit button
def OnSubmit(self, host):
hoster=self.tc.GetValue()
pnmber=self.tc1.GetValue()
wx.MessageBox('Host: %s \nPhone Number : %s' % (hoster, pnmber))
self.Ping(host)
def Ping(host, self):
wmi = win32com.client.GetObject(r"winmgmts:\\.\root\cimv2")
col_items = wmi.ExecQuery("Select * from Win32_PingStatus Where Address = '%s'" % host)
for item in col_items:
if item.StatusCode == None:
return -1
var = 1
ctr = 0
while var == 1:
time.sleep(2)
x = Ping('%s' % (hoster))
if x == -1:
print ctr
ctr = ctr + 1
if ctr == 5:
ctr = 0
recipient = ('%s' % (pnmber))
message = "Sorry to inform you, Your Network is Down"
ser = serial.Serial(4, 460800, timeout=5)
time.sleep(1)
ser.write('ATZ\r')
time.sleep(1)
ser.write('AT+CMGF=1\r')
time.sleep(1)
ser.write('''AT+CMGS="''' + recipient + '''"\r''')
time.sleep(1)
ser.write(message + "\r")
time.sleep(1)
ser.write(chr(26))
time.sleep(1)
ser.close()
exit()
#close button
def OnClose(self, event):
dlg = wx.MessageDialog(self,
"Do you really want to close this application?",
"Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
result = dlg.ShowModal()
dlg.Destroy()
if result == wx.ID_OK:
self.Destroy()
#menuexit button
def onMenuExit(self, event):
self.Close(True)
#closes the window button
def onCloseWindow(self, event):
dlg = wx.MessageDialog(self, "Want to exit?", "Exit", wx.YES_NO | wx.ICON_QUESTION)
if dlg.ShowModal() == wx.ID_YES:
self.Destroy()
dlg.Destroy()
#the frame will always show until press exit
if __name__=='__main__':
app= wx.PySimpleApp()
frame=Samuel(parent=None,id=-1)
frame.Show()
app.MainLoop()
我无法得到它的工作,每当我运行程序....它总是有一个错误发生......不知道为什么