How to create a spin control (Phoenix)
Keywords : Spin control.
Introduction :
This widget lets you increment and decrement a value.
It has two up and down arrow buttons for this purpose.
User can enter a value into a box or increment/decrement it by these two arrows.
wx.SpinCtrl styles :
- wx.SP_ARROW_KEYS
- wx.SP_WRAP
wx.SpinCtrl methods |
|
integer GetValue() |
get the current value |
SetValue(integer value) |
set the current value |
SetValueString(string value) |
something |
SetRange(integer min, integer max) |
set the min and max values |
integer GetMin() |
get the minimum value |
integer GetMax() |
get the maximum value |
(info by ZetCode / Jan Bodnar).
Demonstrating :
Tested py3.x, wx4.x and Win10.
Are you ready to use some samples ?
Test, modify, correct, complete, improve and share your discoveries !
Sample one
sample_one.py is a dialog-based script.
Our main class inherits from wx.Dialog instead of wx.Frame.
The main difference is that we cannot resize the window and we call Destroy() method instead of Close(), when we quit the application.
sample_one script converts Fahrenheit temperature to Celsius.
This example is very popular and can be found in most programming primer books.
1 # sample_one.py
2
3 """
4
5 Author : Jan Bodnar
6 Website : zetcode.com
7
8 """
9
10 import wx
11
12 # class MyDialog
13 # class MyApp
14
15 #---------------------------------------------------------------------------
16
17 class MyDialog(wx.Dialog):
18 def __init__(self, parent, id, title):
19 wx.Dialog.__init__(self, parent, id, title,
20 wx.DefaultPosition, wx.Size(350, 350))
21
22 self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
23
24 #------------
25
26 # wx.Font(pointSize, family, style, weight, underline, faceName)
27 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
28 font.SetWeight(wx.BOLD)
29
30 #------------
31
32 self.convert = wx.StaticText(self, -1,
33 ' Convert Fahrenheit temperature to Celsius ',
34 (20, 20), (-1, -1), wx.ALIGN_CENTER)
35 self.convert.SetFont(font)
36 self.convert.SetForegroundColour('White')
37 self.convert.SetBackgroundColour('Red')
38
39
40 self.Fahrenheit = wx.StaticText(self, -1, 'Fahrenheit : ', (20, 80))
41 self.Fahrenheit.SetFont(font)
42
43 self.celsius = wx.StaticText(self, -1, 'Celsius : ', (20, 150))
44 self.celsius.SetFont(font)
45
46 self.result = wx.StaticText(self, -1, '...............', (150, 150))
47 self.result.SetForegroundColour('Blue')
48
49 self.sc = wx.SpinCtrl(self, -1, '', (150, 75), (60, -1))
50 self.sc.SetRange(-459, 1000)
51 self.sc.SetValue(0)
52
53 compute_btn = wx.Button(self, 1, 'Co&mpute', (70, 250))
54 compute_btn.SetFocus()
55
56 clear_btn = wx.Button(self, 2, '&Close', (185, 250))
57
58 #------------
59
60 self.Bind(wx.EVT_BUTTON, self.OnCompute, id=1)
61 self.Bind(wx.EVT_BUTTON, self.OnClose, id=2)
62 self.Bind(wx.EVT_CLOSE, self.OnClose)
63
64 #-----------------------------------------------------------------------
65
66 def OnCompute(self, event):
67 fahr = self.sc.GetValue()
68 cels = round((fahr-32)*5/9.0, 2)
69 self.result.SetLabel(str(cels))
70
71
72 def OnClose(self, event):
73 self.Destroy()
74
75 #---------------------------------------------------------------------------
76
77 class MyApp(wx.App):
78 def OnInit(self):
79 dlg = MyDialog(None, -1, 'wx.SpinCtrl')
80 dlg.Centre()
81 dlg.Show(True)
82
83 return True
84
85 #---------------------------------------------------------------------------
86
87 app = MyApp(0)
88 app.MainLoop()
Download source
Additional Information
Link :
- - - - -
https://wiki.wxpython.org/TitleIndex
Thanks to
Jan Bodnar (sample_one.py coding), the wxPython community...
About this page
Date(d/m/y) Person (bot) Comments :
28/12/20 - Ecco (Created page for wxPython Phoenix).
Comments
- blah, blah, blah....