Wxpython 简明教程
wxPython - Buttons
按钮组件在任何 GUI 界面中都使用最广泛。它捕获用户生成的单击事件。它最明显的使用是触发绑定到它的一个处理程序函数。
Button widget is most widely used in any GUI interface. It captures the click event generated by the user. Its most obvious use is to trigger a handler function bound to it.
wxPython 类库提供了不同类型的按钮。有一个简单的传统按钮, wx.Button 类对象,它的标题带有一些文本。也有一个二状态按钮,名为 wx.ToggleButton 。它的按下或松开状态可以通过事件处理程序函数来识别。
wxPython class library provides different types of buttons. There is a simple, traditional button, wx.Button class object, which carries some text as its caption. A two-state button is also available, which is named as wx.ToggleButton. Its pressed or depressed state can be identified by eventhandler function.
另一种类型的按钮 wx.BitmapButton ,它的表面显示一个位图(图像)作为图标。
Another type of button, wx.BitmapButton displays a bitmap (image) as icon on its face.
wx.Button 类和 wx.ToggleButton 类的构造函数接收以下参数 −
Constructor for wx.Button class and wx.ToggleButton class takes the following arguments −
Wx.Button(parent, id, label, pos, size, style)
以下是 wx.Button 类的一些重要方法 −
These are some important methods of wx.Button class −
S.N. |
Methods & Description |
1 |
SetLabel() Sets the button’s caption programmatically |
2 |
GetLabel() Returns the button’s caption |
3 |
SetDefault() Button is set to default for the top level window. Emulates the click event on pressing Enter key |
wx.ToggleButton 类的两个重要方法是:
Two important methods of wx.ToggleButton class are −
S.N. |
Methods & Description |
1 |
GetValue() Returns the state of toggle button (on/off) |
2 |
SetValue() Sets the state of button programmatically |
要创建位图按钮,首先需要根据图像文件构建位图对象。
In order to create a bitmap button, firstly, a bitmap object needs to be constructed out of an image file.
以下 wx.Bitmap 类构造函数的变体最常用:
The following variation of wx.Bitmap class constructor is most commonly used −
Wx.Bitmap(fiiename, wx.BITMAP_TYPE)
一些预定义的位图类型常量是:
Some of the predefined bitmap type constants are −
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 类构造函数的参数之一。
This bitmap object is used as one of the parameters for wx.BitmapButton class constructor.
Wx.BitmapButton(parent, id, bitmap, pos, size, style)
在某些操作系统平台上,位图按钮可以同时显示位图和标签。SetLabel() 方法分配标题。在其他平台上,它充当内部标签。
On some OS platforms, the bitmap button can display both bitmap and label. SetLabel() methods assign the caption. On other platforms, it serves as an internal label.
普通按钮以及位图按钮都会发出 wx.CommandEvent。EVT_BUTTON 绑定器会将处理程序功能关联到它。
The normal button as well bitmap button emits a wx.CommandEvent. EVT_BUTTON binder associates a handler function to it.
另一方面,切换按钮使用 wx.TOGGLEBUTTON 绑定器进行事件处理。
The toggle button on the other hand uses wx.TOGGLEBUTTON binder for event handling.
在以下示例中,所有三种类型的按钮都放置在面板的垂直框调整器中。
In the following example, buttons of all three types are placed in a vertical box sizer of a panel.
使用语句创建简单按钮对象:
Simple button object is created using the statement −
self.btn = wx.Button(panel, -1, "click Me")
切换按钮通过以下语句构建:
Toggle button is constructed by following statement −
self.tbtn = wx.ToggleButton(panel , -1, "click to on")
使用以下语句将这些按钮添加到垂直调整器中:
These buttons are added into vertical sizer using the following statements −
vbox.Add(self.btn,0,wx.ALIGN_CENTER)
vbox.Add(self.tbtn,0,wx.EXPAND|wx.ALIGN_CENTER)
Note ——由于 wx.EXPAND 标志,切换按钮占据了框架的整个宽度。
Note − Because of wx.EXPAND flag, the toggle button occupies the entire width of the frame.
使用 EVT_BUTTON 和 EVT_TOGGLEBUTTON 绑定器,它们与各自的处理程序相关联。
Using EVT_BUTTON and EVT_TOGGLEBUTTON binders they are associated with the respective handlers.
self.btn.Bind(wx.EVT_BUTTON,self.OnClicked)
self.tbtn.Bind(wx.EVT_TOGGLEBUTTON,self.OnToggle)
三个位图按钮被添加到水平框调整器中。这些按钮显示图像作为其标题的图标。
Three bitmap buttons are added into a horizontal box sizer. These buttons display an image as icon as their caption.
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))
这三个按钮的单击事件被定向到 OnClicked() 方法。
Click event of these three buttons is directed to OnClicked() method.
self.bmpbtn.Bind(wx.EVT_BUTTON, self.OnClicked)
self.bmpbtn1.Bind(wx.EVT_BUTTON, self.OnClicked)
self.bmpbtn2.Bind(wx.EVT_BUTTON, self.OnClicked)
分别将这些按钮的内部标签设置为 NEW、OPEN 和 SAVE。
Internal labels of these buttons are set to NEW, OPEN and SAVE respectively.
OnClicked() 事件处理函数会检索导致单击事件的源按钮的标签。该标签会打印在控制台上。
OnClicked() event handler function retrieves the label of source button, which caused the click event. That label is printed on the console.
def OnClicked(self, event):
btn = event.GetEventObject().GetLabel()
print "Label of pressed button = ",btn
OnToggle() 事件处理函数会在单击切换按钮时触发。它的状态由 GetValue() 方法读取,按钮的标题会相应地设置。
OnToggle() event handler is triggered when the toggle button is clicked. Its state is read by GetValue() method and accordingly, the button’s caption is set.
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")
完整的代码清单如下 -
The complete code listing is as follows −
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')
app.MainLoop()
上述代码生成以下输出 -
The above code produces the following output −
按下按钮的标签 = 单击我
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