In this Python TKinter article i want to show you How To Create SpinBox In Python
TKinter. if you are interested in Python GUI Development, you can check the below links.
Also you can read more articles on Python GUI Development
1: PyQt5 GUI Development Tutorials
2: Pyside2 GUI Development Tutorials
3: wxPython GUI Development Tutorials
4: Kivy GUI Development Tutorials
So this is the complete source code for How To Create SpinBox In Python TKinter
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 |
from tkinter import * from tkinter import ttk from tkinter import scrolledtext class Root(Tk): def __init__(self): super(Root, self).__init__() self.title("Python Tkinter Spin Box") self.minsize(640, 400) self.wm_iconbitmap('icon.ico') self.spinBox() def spinCallBack(self): value = self.spin.get() print(value) self.scrollText.insert(INSERT, value) def spinBox(self): self.spin = ttk.Spinbox(self, from_ = 0, to = 10, command = self.spinCallBack) self.spin.grid(column = 0, row = 2) scroll_w = 30 scroll_h = 10 self.scrollText = scrolledtext.ScrolledText(self, width=scroll_w, height=scroll_h, wrap=WORD) self.scrollText.grid(column =1, row = 2) root = Root() root.mainloop() |
These are the imports that we need for this article, basically it is tkinter library, also we are going
to import ttk from tkinter with scrolledtext from tkinter .
1 2 3 |
from tkinter import * from tkinter import ttk from tkinter import scrolledtext |
So at the top we have our Root class that inherits from TK class, and in that class we have added
some requirements for the window like title, size and also icon. also we have added our
spinBox() method in here.
1 2 3 4 5 6 7 8 |
class Root(Tk): def __init__(self): super(Root, self).__init__() self.title("Python Tkinter Spin Box") self.minsize(640, 400) self.wm_iconbitmap('icon.ico') self.spinBox() |
This is callback method for our spinbox, in this method we get the value from spinbox and we
are going to set that value to our ScrollText.
1 2 3 4 |
def spinCallBack(self): value = self.spin.get() print(value) self.scrollText.insert(INSERT, value) |
This is the creation of our SpinBox and ScrollText.
1 2 3 4 5 6 7 8 9 10 11 |
def spinBox(self): self.spin = ttk.Spinbox(self, from_ = 0, to = 10, command = self.spinCallBack) self.spin.grid(column = 0, row = 2) scroll_w = 30 scroll_h = 10 self.scrollText = scrolledtext.ScrolledText(self, width=scroll_w, height=scroll_h, wrap=WORD) self.scrollText.grid(column =1, row = 2) |
Run the code and this will be the result
Also you can watch the complete video tutorial for this article
Subscribe and Get Free Video Courses & Articles in your Email