1. 安装好wxPython
2.编写程序哈哈哈哈哈哈
import wx
class ButtonFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,"ButtonFrame",size=(500,200))
panel = wx.Panel(self,-1)
self.button = wx.Button(panel,-1,"Hello",pos=(10,20))
self.Bind(wx.EVT_BUTTON,self.OnClick,self.button) # Bind(event,handler,soure = None,id=wx.ID_ANY,id2,wx.ANY)
self.button.SetDefault()
def OnClick(self, event):
self.button.SetLabel("Clicked")# when clicked,button label changed to "Clicked"
if __name__ == '__main__':
app = wx.App()# create an APP,every wxpython is a living example,if you want a complex APP,use "class" to extend
frame = ButtonFrame() # complicate is "wx.Frame(parent,ID,title)",;ID is -1 default;
frame.Show()# show the Frame
app.MainLoop()# run the APP
class中定义比较复杂的函数
wx.Frame原型
Frame(parent, id=ID_ANY, title="", pos=DefaultPosition, size=DefaultSize, style=DEFAULT_FRAME_STYLE, name=FrameNameStr)
Panel是面板,wx.Button增加一个按钮
Bind()连接事件,原型wx.Frame.Bind(self, event, handler, source=None, id=-1, id2=-1)
基本上只会给定event、handler和source,event是事件的名称,handler是处理函数,source是事件的发生者,self.Bind(wx.EVT_BUTTON, self.OnButton1, self.Button1)表示的是:一个Button1被单击而发生了EVT_BUTTON事件,用self.OnButton1()来处理,self.OnButton1()是事件处理函数