wxPython類庫提供不同型別的按鈕。有一個簡單的、傳統的按鈕,wx.Button類物件帶有一些文字作為其標題。 兩狀態的按鈕也可以,它被命名為wx.ToggleButton。其按下或消沉狀態可通過事件處理程式函式來識別。
Wx.Button(parent, id, label, pos, size, style)
S.N. |
方法和說明
|
---|---|
1 |
SetLabel()
程式設計設定按鈕的標題
|
2 |
GetLabel()
返回按鈕的標題
|
3 |
SetDefault()
按鈕設定為預設頂層視窗。模擬click事件按下確認鍵
|
S.N. |
方法和說明
|
---|---|
1 |
GetValue() Returns the state of toggle button (on/off) |
2 |
SetValue() Sets the state of button programmatically |
Wx.Bitmap(fiiename, wx.BITMAP_TYPE)
wx.BITMAP_TYPE_BMP |
wx.BITMAP_TYPE_ICO |
wx.BITMAP_TYPE_CUR |
wx.BITMAP_TYPE_TIFF |
wx.BITMAP_TYPE_TIF |
wx.BITMAP_TYPE_GIF |
wx.BITMAP_TYPE_PNG |
wx.BITMAP_TYPE_JPEG |
wx.BITMAP_TYPE_PCX |
wx.BITMAP_TYPE_ICON |
wx.BITMAP_TYPE_ANY |
Wx.BitmapButton(parent, id, bitmap, pos, size, style)
在某些作業系統平台上,點陣圖按鈕可以顯示點陣圖和標籤。 SetLabel()方法用於指定標題。在其他平台上,它作為一個內部的標籤。正常的按鈕,以及點陣圖按鈕發出wx.CommandEvent。 EVT_BUTTON系結器關聯一個函式來做處理。
self.btn = wx.Button(panel, -1, "click Me")
self.tbtn = wx.ToggleButton(panel , -1, "click to on")
vbox.Add(self.btn,0,wx.ALIGN_CENTER) vbox.Add(self.tbtn,0,wx.EXPAND|wx.ALIGN_CENTER)
self.btn.Bind(wx.EVT_BUTTON,self.OnClicked) self.tbtn.Bind(wx.EVT_TOGGLEBUTTON,self.OnToggle)
bmp = wx.Bitmap("NEW.BMP", wx.BITMAP_TYPE_BMP) self.bmpbtn = wx.BitmapButton(panel, id = wx.ID_ANY, bitmap = bmp, size = (bmp.GetWidth()+10, bmp.GetHeight()+10)) bmp1 = wx.Bitmap("OPEN.BMP", wx.BITMAP_TYPE_BMP) self.bmpbtn1 = wx.BitmapButton(panel, id = wx.ID_ANY, bitmap = bmp1, size = (bmp.GetWidth()+10, bmp.GetHeight()+10)) bmp2 = wx.Bitmap("SAVE.BMP", wx.BITMAP_TYPE_BMP) self.bmpbtn2 = wx.BitmapButton(panel, id = wx.ID_ANY, bitmap = bmp2, size = (bmp.GetWidth()+10, bmp.GetHeight()+10))
self.bmpbtn.Bind(wx.EVT_BUTTON, self.OnClicked) self.bmpbtn1.Bind(wx.EVT_BUTTON, self.OnClicked) self.bmpbtn2.Bind(wx.EVT_BUTTON, self.OnClicked)
def OnClicked(self, event): btn = event.GetEventObject().GetLabel() print "Label of pressed button = ",btn
def OnToggle(self,event): state = event.GetEventObject().GetValue() if state == True: print "off" event.GetEventObject().SetLabel("click to off") else: print "on" event.GetEventObject().SetLabel("click to on")
import wx class Mywin(wx.Frame): def __init__(self, parent, title): super(Mywin, self).__init__(parent, title = title,size = (200,150)) panel = wx.Panel(self) vbox = wx.BoxSizer(wx.VERTICAL) self.btn = wx.Button(panel,-1,"click Me") vbox.Add(self.btn,0,wx.ALIGN_CENTER) self.btn.Bind(wx.EVT_BUTTON,self.OnClicked) self.tbtn = wx.ToggleButton(panel , -1, "click to on") vbox.Add(self.tbtn,0,wx.EXPAND|wx.ALIGN_CENTER) self.tbtn.Bind(wx.EVT_TOGGLEBUTTON,self.OnToggle) hbox = wx.BoxSizer(wx.HORIZONTAL) bmp = wx.Bitmap("NEW.BMP", wx.BITMAP_TYPE_BMP) self.bmpbtn = wx.BitmapButton(panel, id = wx.ID_ANY, bitmap = bmp, size = (bmp.GetWidth()+10, bmp.GetHeight()+10)) hbox.Add(self.bmpbtn,0,wx.ALIGN_CENTER) self.bmpbtn.Bind(wx.EVT_BUTTON,self.OnClicked) self.bmpbtn.SetLabel("NEW") bmp1 = wx.Bitmap("OPEN.BMP", wx.BITMAP_TYPE_BMP) self.bmpbtn1 = wx.BitmapButton(panel, id = wx.ID_ANY, bitmap = bmp1, size = (bmp.GetWidth()+10, bmp.GetHeight()+10)) hbox.Add(self.bmpbtn1,0,wx.ALIGN_CENTER) self.bmpbtn1.Bind(wx.EVT_BUTTON,self.OnClicked) self.bmpbtn1.SetLabel("OPEN") bmp2 = wx.Bitmap("SAVE.BMP", wx.BITMAP_TYPE_BMP) self.bmpbtn2 = wx.BitmapButton(panel, id = wx.ID_ANY, bitmap = bmp2, size = (bmp.GetWidth()+10, bmp.GetHeight()+10)) hbox.Add(self.bmpbtn2,0,wx.ALIGN_CENTER) self.bmpbtn2.Bind(wx.EVT_BUTTON,self.OnClicked) self.bmpbtn2.SetLabel("SAVE") vbox.Add(hbox,1,wx.ALIGN_CENTER) panel.SetSizer(vbox) self.Centre() self.Show() self.Fit() def OnClicked(self, event): btn = event.GetEventObject().GetLabel() print "Label of pressed button = ",btn def OnToggle(self,event): state = event.GetEventObject().GetValue() if state == True: print "Toggle button state off" event.GetEventObject().SetLabel("click to off") else: print " Toggle button state on" event.GetEventObject().SetLabel("click to on") app = wx.App() Mywin(None, 'Button Demo - www.tw511.com') app.MainLoop()
Label of pressed button = click Me
Toggle button state off
Toggle button state on
Label of pressed button = NEW
Label of pressed button = OPEN
Label of pressed button = SAVE