In this wxPython article i want to show you How To Create SpinButton in wxPython. A wx.SpinButton has two small up and down (or left and right) arrow buttons. So it us used next to a text control. and you can increment and decrements the value
SpinButton class supports the following styles:
- wx.SP_HORIZONTAL: Specifies a horizontal spin button (note that this style is not supported in wxGTK).
- wx.SP_VERTICAL: Specifies a vertical spin button.
- wx.SP_ARROW_KEYS: The user can use arrow keys to change the value.
- wx.SP_WRAP: The value wraps at the minimum and maximum.
How to Create SpinButton in wxPython?
Let’s create our example, This is the complete code for How To Create SpinButton in wxPython.
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 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): # Initialize the frame super(MyFrame, self).__init__(parent, title=title, size=(300, 150)) # Set window icon self.SetIcon(wx.Icon('codeloop.png')) # Create panel self.panel = MyPanel(self) class MyPanel(wx.Panel): def __init__(self, parent): # Initialize the panel super(MyPanel, self).__init__(parent) # Create text control for displaying the value self.text = wx.TextCtrl(self, -1, "1", pos=(30, 50), size=(60, -1)) # Calculate position for the spin button h = self.text.GetSize().height w = self.text.GetSize().width + self.text.GetPosition().x + 2 # Create a spin button self.spinButton = wx.SpinButton(self, -1, pos=(w, 50), size=(h * 2 / 3, h), style=wx.SP_VERTICAL) self.spinButton.SetRange(1, 100) self.spinButton.SetValue(1) # Bind spin event to the handler method self.Bind(wx.EVT_SPIN, self.OnSpin, self.spinButton) def OnSpin(self, event): # Set the value of the text control self.text.SetValue(str(event.GetPosition())) class MyApp(wx.App): def OnInit(self): # Initialize the application self.frame = MyFrame(parent=None, title="Codeloop - wxPython SpinButton") self.frame.Show() # Show the frame return True app = MyApp() # Start the application event loop app.MainLoop() |
So this class is MyFrame class that inherits from wx.Frame and it is a top level window that contains our panel.
1 2 3 4 5 6 7 8 9 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title) self.panel = MyPanel(self) |
After that we have MyPanel class that inherits from wx.Panel and it is the place that we create our widgets and layouts in this class. You can see that we have created the SpinButton in this class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) self.text = wx.TextCtrl(self, -1, "1", (30, 50), (60, -1)) h = self.text.GetSize().height w = self.text.GetSize().width + self.text.GetPosition().x + 2 self.spinButton = wx.SpinButton(self, -1, (w, 50), (h * 2 / 3, h), wx.SP_VERTICAL) self.spinButton.SetRange(1,100) self.spinButton.SetValue(1) self.Bind(wx.EVT_SPIN, self.OnSpin, self.spinButton) |
So this is the TextCtrl that i have created for my SpinButton
1 |
self.text = wx.TextCtrl(self, -1, "1", (30, 50), (60, -1)) |
These are the height and width for our SpinButton
1 2 |
h = self.text.GetSize().height w = self.text.GetSize().width + self.text.GetPosition().x + 2 |
And now this is the SpinButton creation
1 2 3 4 |
self.spinButton = wx.SpinButton(self, -1, (w, 50), (h * 2 / 3, h), wx.SP_VERTICAL) |
These are the range and value of our SpinButton
1 2 |
self.spinButton.SetRange(1,100) self.spinButton.SetValue(1) |
In here we bind an event with our SpinButton
1 |
self.Bind(wx.EVT_SPIN, self.OnSpin, self.spinButton) |
And this is the event method
1 2 |
def OnSpin(self, event): self.text.SetValue(str(event.GetPosition())) |
So the last class is MyApp class that inherits from wx.App. the OnInit() method is where you will most often create frame subclass objects. and start our main loop.That’s it. Once the application’s main event loop processing takes over, control passes to wxPython. Unlike procedural programs, a wxPython GUI program primarily responds to the events taking place around it, mostly determined by a human user clicking with a mouse and typing at the keyboard. When all the human user clicking with a mouse and typing at the keyboard. When all the return and the program will exit.
1 2 3 4 5 6 7 8 9 10 |
class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Codeloop.org - SpinButton") self.frame.Show() return True app = MyApp() app.MainLoop() |
So run the code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email