在wxpython中核心部件wx.spinctrl可以增加或减少一个数值。
为此,它有着两个分别向上或向下的箭头按钮。用户可以在一个框中输入一个数值,或是通过这两个箭头来增加或减少这个数值。
专题教程:wxpython中文教程
wx.SpinCtrl 样式有:
wx.SP_ARROW_KEYS
wx.SP_WRAP
例子:
 
#!/usr/bin/python
#coding=utf-8
#spinctrl.py
import wx
class MyDialog(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, 
                           wx.DefaultPosition, wx.Size(350, 310))
        
        wx.StaticText(self, -1, '将法式温度转换为摄氏温度', (20, 20))
        wx.StaticText(self, -1, '法式温度:', (20, 80))
        wx.StaticText(self, -1, '摄氏温度:', (20, 150))
        
        self.celsius = wx.StaticText(self, -1, '', (150, 150))
        self.sc = wx.SpinCtrl(self, -1, '', (150, 75), (60, -1))
        self.sc.SetRange(-459, 1000)
        self.sc.SetValue(0)
        
        compute_btn = wx.Button(self, 1, '计 算', (70, 250))
        compute_btn.SetFocus()
        clear_btn = wx.Button(self, 2, '关 闭', (185, 250))
        
        self.Bind(wx.EVT_BUTTON, self.OnCompute, id=1)
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=2)
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        
    def OnCompute(self, event):
        fahr = self.sc.GetValue()
        cels = round((fahr-32)*5/9.0, 2)
        self.celsius.SetLabel(str(cels))
        
    def OnClose(self, event):
        self.Destroy()        
        
class MyApp(wx.App):
    def OnInit(self):
        dlg = MyDialog(None, -1, 'spinctrl.py')
        dlg.Show(True)
        dlg.Center()
        return True
app = MyApp(0)
app.MainLoop()
 
程序 spinctrl.py 是一个基于对话框的脚本. 这里主要的类继承自 wx.Dialog,而不是 wx.Frame.
区别在于不能缩放窗口,以及在退出应用时,调用 Destroy() 而不是 Close() 方法. 它的功能是将法式温度转换为摄氏温度。
如图:
