article

Saturday, June 4, 2016

wxpython widgets wx.SpinCtrl

wxpython widgets wx.SpinCtrl

The wx.SpinCtrl widget lets us increment and decrement a value. It has two up and down arrow buttons for this purpose. 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import wx
 
 
class Example(wx.Frame):
            
    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw)
         
        self.InitUI()
         
    def InitUI(self):  
 
        pnl = wx.Panel(self)
 
         
        wx.StaticText(self, label='Convert Fahrenheit temperature to Celsius',
            pos=(20,20))
        wx.StaticText(self, label='Fahrenheit: ', pos=(20, 80))
        wx.StaticText(self, label='Celsius: ', pos=(20, 150))
         
        self.celsius = wx.StaticText(self, label='', pos=(150, 150))
        self.sc = wx.SpinCtrl(self, value='0', pos=(150, 75), size=(60, -1))
        self.sc.SetRange(-459, 1000)
         
        btn = wx.Button(self, label='Compute', pos=(70, 230))
        btn.SetFocus()
        cbtn = wx.Button(self, label='Close', pos=(185, 230))
 
        btn.Bind(wx.EVT_BUTTON, self.OnCompute)
        cbtn.Bind(wx.EVT_BUTTON, self.OnClose)
            
        self.SetSize((350, 310))
        self.SetTitle('wx.SpinCtrl')
        self.Centre()
        self.Show(True)         
         
    def OnClose(self, e):
         
        self.Close(True)   
         
    def OnCompute(self, e):
         
        fahr = self.sc.GetValue()
        cels = round((fahr - 32) * 5 / 9.0, 2)
        self.celsius.SetLabel(str(cels))       
                       
def main():
     
    ex = wx.App()
    Example(None)
    ex.MainLoop()   
 
if __name__ == '__main__':
    main()  

Related Post