关于wxpython输入IP地址的控件,在网上资源比较少,且不好找,所以记录一下~
#coding=utf-8 import wx import wx.lib.masked as masked class MyFrame(wx.Frame): def __init__(self, parent, id): wx.Frame.__init__(self, parent, id, u'IpAddrCtrl', size = (-1, -1)) panel = wx.Panel(self) bs_top = wx.BoxSizer(wx.HORIZONTAL) bs_center = wx.BoxSizer(wx.HORIZONTAL) self.ip_label = wx.StaticText(panel ,wx.ID_ANY,"IP地址") self.ip= masked.IpAddrCtrl(panel, wx.ID_ANY) bs_top.Add(self.ip_label,proportion=0, flag=wx.ALL, border=5) bs_top.Add(self.ip,proportion=0, flag=wx.ALL, border=5) self.new_ip_label = wx.StaticText(panel ,wx.ID_ANY,"填写IP:") self.new_ip = masked.IpAddrCtrl(panel, wx.ID_ANY) self.set_btn = wx.Button(panel,label='设定') self.Bind(wx.EVT_BUTTON,self.SetIPButton, self.set_btn) bs_center.Add(self.new_ip_label,proportion=0, flag=wx.ALL, border=5) bs_center.Add(self.new_ip,proportion=0, flag=wx.ALL, border=5) bs_center.Add(self.set_btn,proportion=0, flag=wx.ALL, border=5) bs_all = wx.BoxSizer(wx.VERTICAL) bs_all.Add(bsizer_top,proportion=0, flag=wx.ALL, border=5) bs_all.Add(bsizer_center,proportion=0, flag=wx.ALL, border=5) panel.SetSizer(bs_all) def SetIPButton(self,event): self.ip.SetValue(self.new_ip.GetValue()) if __name__ == '__main__': app = wx.App() frame = MyFrame(None,-1) frame.Show() frame.Center() app.MainLoop()
因为没有对IP地址编辑框进行输入内容的校验,所以可以输入任意内容。(想要校验输入字符是否符合规定,可以试试 wx.Validator 用法)