Sample1,创建一个窗体控件:
import wx //导入wxPython库
class Frame(wx.Frame): // 创建一个窗体类
pass //该窗体什么也不做
class App(wx.App): // 创建一个App类
def OnInit(self): //初始化
self.frame = Frame(parent=None, title='Spare') //初始化窗体标题“Spare”
self.frame.Show() //窗体显示
self.SetTopWindow(self.frame) //设置窗体属性
return True //返回
if __name__ =='__main__':
app = App() //实例化一个App
app.MainLoop() //App无限循环
Sample2,窗体添加一个背景图片
import wx
class Frame(wx.Frame): //定义Frame类
def __init__(self, image,parent=None, id=-1,pos=wx.DefaultPosition, title='Hello, wxPython!'): //窗体标题“Hello wxPyhton!”
temp = image.ConvertToBitmap()
size = temp.GetWidth(), temp.GetHeight()
wx.Frame.__init__(self, parent, id, title, pos, size)
self.bmp = wx.StaticBitmap(parent=self, bitmap=temp)
self.SetClientSize(size)
class App(wx.App): //定义App类
def OnInit(self): // 初始化App类
image = wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG) //加载图片
self.frame = Frame(image) //窗体
self.frame.Show() //窗体显示
self.SetTopWindow(self.frame)
return True
def main(): // 定义main()函数
app = App() // 实例化App()
app.MainLoop() // App无限循环
if __name__ =='__main__':
main() //运行main()函数
Sample3,菜单和状态栏
import wx
class MyApp(wx.App): //定义MyApp()类
def OnInit(self): //初始化
frame = MyFrame("Hello World", (50, 60), (450, 340)) //实例化MyFrame,标题“Hello World”
frame.Show() //窗体显示
self.SetTopWindow(frame)
return True
class MyFrame(wx.Frame): //创建MyFrame类
def __init__(self, title, pos, size): //初始化
wx.Frame.__init__(self, None, -1, title, pos, size)
menuFile = wx.Menu() //实例化Menu
menuFile.Append(1, "&About...") //添加“About”
menuFile.AppendSeparator() //添加分割线
menuFile.Append(2, "E&xit") //添加“Eexit”
menuBar = wx.MenuBar()// 添加菜单栏
menuBar.Append(menuFile, "&File")//添加“File”
self.SetMenuBar(menuBar)//设置菜单栏
self.CreateStatusBar()//创建状态栏
self.SetStatusText("Welcome to wxPython!")//设置状态栏信息
self.Bind(wx.EVT_MENU, self.OnAbout, id=1)//事件绑定
self.Bind(wx.EVT_MENU, self.OnQuit, id=2)//事件绑定
def OnQuit(self, event):// 菜单“Quit”触发的事件
self.Close()
def OnAbout(self, event): //事件,菜单“About”触发的事件
wx.MessageBox("This is a wxPython Hello world sample", "About Hello World", wx.OK | wx.ICON_INFORMATION, self)
if __name__ =='__main__':
app = MyApp(False) //实例化MyApp();
app.MainLoop()
Sample4,按钮
import wx
class TwoButtonEvent(wx.PyCommandEvent): //定义事件类TwoButtonEvent
def __init__(self, evtType, id)://初始化
wx.PyCommandEvent.__init__(self, evtType, id)
self.clickCount =0 // 计数为“0”
def GetClickCount(self): //定义函数GetClickCount(),返回计数值
return self.clickCount
def SetClickCount(self, count): // 定义函数SetClickCount(),设定计数值
self.clickCount = count
myEVT_TWO_BUTTON = wx.NewEventType() //事件
EVT_TWO_BUTTON = wx.PyEventBinder(myEVT_TWO_BUTTON, 1) //事件绑定
class TwoButtonPanel(wx.Panel): //定义按钮界面
def __init__(self, parent, id=-1, leftText="Left", rightText="Right"): //初始化按钮名
wx.Panel.__init__(self, parent, id)
self.leftButton = wx.Button(self, label=leftText) //实例化左按钮
self.rightButton = wx.Button(self, label=rightText,pos=(100,0)) //实例化右按钮
self.leftClick =False
self.rightClick =False
self.clickCount =0
self.leftButton.Bind(wx.EVT_LEFT_DOWN, self.OnLeftClick)//按钮事件绑定
self.rightButton.Bind(wx.EVT_LEFT_DOWN, self.OnRightClick)//按钮事件绑定
def OnLeftClick(self, event): //定义左键单击事件
self.leftClick =True
self.OnClick()
event.Skip()
def OnRightClick(self, event): //定义右键单击事件
self.rightClick =True
self.OnClick()
event.Skip()
def OnClick(self): //定义单击事件
self.clickCount +=1
if self.leftClickand self.rightClick:
self.leftClick =False
self.rightClick =False
evt = TwoButtonEvent(myEVT_TWO_BUTTON, self.GetId())
evt.SetClickCount(self.clickCount)
self.GetEventHandler().ProcessEvent(evt)
class CustomEventFrame(wx.Frame): //创建事件窗体CustomEventFrame()
def __init__(self, parent, id): //初始化
wx.Frame.__init__(self, parent, id, 'Click Count: 0', size=(300, 100))
panel = TwoButtonPanel(self) //实例化 TwoButtonPanel()
self.Bind(EVT_TWO_BUTTON, self.OnTwoClick, panel) //事件绑定
def OnTwoClick(self, event): //定义事件
self.SetTitle("Click Count: %s" % event.GetClickCount()) //标题显示计数
if __name__ =='__main__':
app = wx.PySimpleApp() //实例化wx.PySimpleApp()
frame = CustomEventFrame(parent=None, id=-1) //实例化窗体CustomEventFrame
frame.Show() //窗体显示
app.MainLoop()