# -*- coding: UTF-8 -*-
#!/usr/bin/env python
import wx
class MyFrame(wx.Frame):
"""
This is MyFrame. It just shows a few controls on a wxPanel,
and has a simple menu.
"""
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title,
pos=(250, 250), size=(550,270))
# Create the menubar
menuBar = wx.MenuBar()
# and a menu
menu = wx.Menu()
# add an item to the menu, using \tKeyName automatically
# creates an accelerator, the third param is some help text
# that will show up in the statusbar
menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")
# bind the menu event to an event handler
self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)
# and put the menu on the menubar
menuBar.Append(menu, "&File")
self.SetMenuBar(menuBar)
self.CreateStatusBar()
# Now create the Panel to put the other controls on.
panel = wx.Panel(self)
self.panel = panel
# and a few controls
text = wx.StaticText(panel, -1, "Input:")
text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
text.SetSize(text.GetBestSize())
spacetext = wx.StaticText(panel, -1, "")
spacetext.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
spacetext.SetSize(text.GetBestSize())
# add help info
textip = wx.StaticText(panel, -1, "IP:")
textip.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
textip.SetSize(text.GetBestSize())
textmask = wx.StaticText(panel, -1, "MASK:")
textmask.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
textmask.SetSize(text.GetBestSize())
textsubnet = wx.StaticText(panel, -1, "SUBNET:")
textsubnet.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
textsubnet.SetSize(text.GetBestSize())
#btn = wx.Button(panel, -1, "Close")
#funbtn = wx.Button(panel, -1, "Just for fun...")
calbtn = wx.Button(panel, -1, "Calculate")
# bind the button events to handlers
#self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, btn)
#self.Bind(wx.EVT_BUTTON, self.OnFunButton, funbtn)
self.Bind(wx.EVT_BUTTON, self.OnCalButton, calbtn)
#add a ip
txtip = wx.TextCtrl(panel, -1, "0.0.0.0", size=(125, -1))
txtip.SetInsertionPoint(0)
panel.txtip = txtip
#error ???
#panel.Bind(wx.EVT_TEXT, panel.EvtText, txtip)
#txtip.Bind(wx.EVT_CHAR, panel.EvtChar)
#txtip.Bind(wx.EVT_SET_FOCUS, panel.OnSetFocus)
#txtip.Bind(wx.EVT_KILL_FOCUS, panel.OnKillFocus)
#txtip.Bind(wx.EVT_WINDOW_DESTROY, panel.OnWindowDestroy)
#add ip mask
txtipmask = wx.TextCtrl(panel, -1, "255.255.255.255", pos=wx.Point(100, 100), size=(125, -1))
txtipmask.SetInsertionPoint(0)
panel.txtipmask = txtipmask
#add subnet ip
txtsubnet = wx.TextCtrl(panel, -1, "", size=(125, -1))
txtsubnet.SetInsertionPoint(0)
panel.txtsubnet = txtsubnet
txtsubnet.SetEditable(False)
#add binary ip
binip = wx.TextCtrl(panel, -1, "00000000.00000000.00000000.00000000", size=(250, -1))
binip.SetInsertionPoint(0)
panel.binip = binip
binip.SetEditable(False)
#add binary mask
binipmask = wx.TextCtrl(panel, -1, "00000000.00000000.00000000.00000000", size=(250, -1))
binipmask.SetInsertionPoint(0)
panel.binipmask = binipmask
binipmask.SetEditable(False)
#add subnet
binsubnet = wx.TextCtrl(panel, -1, "00000000.00000000.00000000.00000000", size=(250, -1))
binsubnet.SetInsertionPoint(0)
panel.binsubnet = binsubnet
binsubnet.SetEditable(False)
# Use a sizer to layout the controls, stacked vertically and with
# a 10 pixel border around each
#sizer = wx.BoxSizer(wx.VERTICAL)
#sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer = wx.FlexGridSizer(3, 3, 0, 0)
sizer.Add(text, 0, wx.ALL, 10)
sizer.Add(spacetext, 0, wx.ALL, 10)
#sizer.Add(btn, 0, wx.ALL, 10)
#sizer.Add(funbtn, 0, wx.ALL, 10)
sizer.Add(calbtn, 0, wx.ALL, 10)
sizer.Add(textip, 0, wx.ALL, 10)
sizer.Add(txtip, 0, wx.ALL, 10)
sizer.Add(binip, 0, wx.ALL, 10)
sizer.Add(textmask, 0, wx.ALL, 10)
sizer.Add(txtipmask, 0, wx.ALL, 10)
sizer.Add(binipmask, 0, wx.ALL, 10)
sizer.Add(textsubnet, 0, wx.ALL, 10)
sizer.Add(txtsubnet, 0, wx.ALL, 10)
sizer.Add(binsubnet, 0, wx.ALL, 10)
panel.SetSizer(sizer)
panel.Layout()
def OnTimeToClose(self, evt):
"""Event handler for the button click."""
#print "See ya later!"
self.Close()
def OnFunButton(self, evt):
"""Event handler for the button click."""
#print "Having fun yet?"
def OnCalButton(self, evt):
""" calculate the subnet """
input_ip = self.panel.txtip.GetValue()
input_mask = self.panel.txtipmask.GetValue()
subnetbyte = self.calSubnet(input_ip, input_mask)
subnetbyte = [str(s) for s in subnetbyte]
subnet = '.'.join(subnetbyte)
self.panel.txtsubnet.Clear()
self.panel.txtsubnet.WriteText(subnet)
#convert to binary string
ipbyte = input_ip.split('.')
maskbyte = input_mask.split('.')
bin_ip_lst = [str(self.bin(b)) for b in ipbyte]
bin_mask_lst = [str(self.bin(b)) for b in maskbyte]
bin_subnet_lst = [str(self.bin(b)) for b in subnetbyte]
bin_ip = '.'.join(bin_ip_lst)
bin_mask = '.'.join(bin_mask_lst)
bin_subnet = '.'.join(bin_subnet_lst)
self.panel.binip.Clear()
self.panel.binip.WriteText(bin_ip)
self.panel.binipmask.Clear()
self.panel.binipmask.WriteText(bin_mask)
self.panel.binsubnet.Clear()
self.panel.binsubnet.WriteText(bin_subnet)
return
def calSubnet(self, ip, mask):
""" ip and mask"""
ipbyte = ip.split('.')
maskbyte = mask.split('.')
if len(ipbyte) != 4:
print 'error ip address'
if len(maskbyte) != 4:
print 'error mask address'
#wait ,to judge is the digital number
subnetbyte = []
for i in xrange(4):
subnetbyte.append(int(ipbyte[i]) & int(maskbyte[i]))
#print subnetbyte
return subnetbyte
def bin(self, x):
result = ''
x = int(x)
while x > 0:
mod = x % 2
x /= 2
result = str(mod) + result
result = result.rjust(8, '0')
return result
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, "IP Subnet Calculator")
self.SetTopWindow(frame)
#print "Print statements go to this stdout window by default."
frame.Show(True)
return True
'''
class App(wx.App):
def OnInit(self):
frame = wx.MyFrame(parent=None, title='IP子网计算')
frame.Show()
return True
'''
app = MyApp()
app.MainLoop()