Sample1梳杏,創(chuàng)建一個(gè)窗體控件:
import wx? ? //導(dǎo)入wxPython庫(kù)
class Frame(wx.Frame): // 創(chuàng)建一個(gè)窗體類(lèi)
????????????pass? //該窗體什么也不做
class App(wx.App):? // 創(chuàng)建一個(gè)App類(lèi)
????????????def OnInit(self): //初始化
????????????????????????self.frame = Frame(parent=None, title='Spare')? //初始化窗體標(biāo)題“Spare”
????????????????????????self.frame.Show() //窗體顯示
????????????????????????self.SetTopWindow(self.frame) //設(shè)置窗體屬性
????????????????????????return True //返回
if __name__ =='__main__':
????????????app = App() //實(shí)例化一個(gè)App
????????????app.MainLoop() //App無(wú)限循環(huán)
Sample2,窗體添加一個(gè)背景圖片
import wx
class Frame(wx.Frame): //定義Frame類(lèi)
????????????def __init__(self, image,parent=None, id=-1,pos=wx.DefaultPosition, title='Hello, wxPython!'): //窗體標(biāo)題“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類(lèi)
????????????def OnInit(self): // 初始化App類(lèi)
????????????????????????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()函數(shù)
????????????app = App() // 實(shí)例化App()
????????????app.MainLoop() // App無(wú)限循環(huán)
if __name__ =='__main__':
????????????main()? //運(yùn)行main()函數(shù)
Sample3淹接,菜單和狀態(tài)欄
import wx
class MyApp(wx.App): //定義MyApp()類(lèi)
????????????def OnInit(self): //初始化
????????????????????????frame = MyFrame("Hello World", (50, 60), (450, 340)) //實(shí)例化MyFrame十性,標(biāo)題“Hello World”
????????????????????????frame.Show() //窗體顯示
????????????????????????self.SetTopWindow(frame)
????????????????????????return True
class MyFrame(wx.Frame): //創(chuàng)建MyFrame類(lèi)
????????????def __init__(self, title, pos, size): //初始化
????????????????????????wx.Frame.__init__(self, None, -1, title, pos, size)
????????????????????????menuFile = wx.Menu() //實(shí)例化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)//設(shè)置菜單欄
????????????????????????self.CreateStatusBar()//創(chuàng)建狀態(tài)欄
????????????????????????self.SetStatusText("Welcome to wxPython!")//設(shè)置狀態(tài)欄信息
????????????????????????self.Bind(wx.EVT_MENU, self.OnAbout, id=1)//事件綁定
????????????????????????self.Bind(wx.EVT_MENU, self.OnQuit, id=2)//事件綁定
? ? ? ? ? ? def OnQuit(self, event):// 菜單“Quit”觸發(fā)的事件
????????????????????????self.Close()?
????????????def OnAbout(self, event): //事件,菜單“About”觸發(fā)的事件
????????????????????????wx.MessageBox("This is a wxPython Hello world sample", "About Hello World", wx.OK | wx.ICON_INFORMATION, self)
if __name__ =='__main__':
????????????app = MyApp(False)? //實(shí)例化MyApp();
????????????app.MainLoop()
Sample4塑悼,按鈕
import wx
class TwoButtonEvent(wx.PyCommandEvent): //定義事件類(lèi)TwoButtonEvent
????????????def __init__(self, evtType, id)://初始化
????????????????????????wx.PyCommandEvent.__init__(self, evtType, id)
????????????????????????self.clickCount =0 // 計(jì)數(shù)為“0”
????????????def GetClickCount(self): //定義函數(shù)GetClickCount()劲适,返回計(jì)數(shù)值
????????????????????????return self.clickCount
????????????def SetClickCount(self, count): // 定義函數(shù)SetClickCount(),設(shè)定計(jì)數(shù)值
????????????????????????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) //實(shí)例化左按鈕
????????????????????????self.rightButton = wx.Button(self, label=rightText,pos=(100,0)) //實(shí)例化右按鈕
????????????????????????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): //創(chuàng)建事件窗體CustomEventFrame()
????????????def __init__(self, parent, id): //初始化
????????????????????????wx.Frame.__init__(self, parent, id, 'Click Count: 0', size=(300, 100))
????????????????????????panel = TwoButtonPanel(self) //實(shí)例化 TwoButtonPanel()
????????????????????????self.Bind(EVT_TWO_BUTTON, self.OnTwoClick, panel) //事件綁定
????????????def OnTwoClick(self, event): //定義事件
????????????????????????self.SetTitle("Click Count: %s" % event.GetClickCount()) //標(biāo)題顯示計(jì)數(shù)
if __name__ =='__main__':
????????????app = wx.PySimpleApp() //實(shí)例化wx.PySimpleApp()
????????????frame = CustomEventFrame(parent=None, id=-1) //實(shí)例化窗體CustomEventFrame
????????????frame.Show() //窗體顯示
????????????app.MainLoop()?