EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Tkinter Tutorial Tkinter Spinbox
Secondary Sidebar
Tkinter Tutorial
  • Tkinter
    • Python Tkinter
    • Tkinter Widgets
    • Tkinter background image
    • Tkinter button color
    • Tkinter place
    • Python Tkinter Button
    • Python Tkinter Canvas
    • Tkinter Frame
    • Tkinter LabelFrame
    • Python Tkinter Label
    • Tkinter Text
    • Tkinter Scrollbar
    • Tkinter Listbox
    • Tkinter Spinbox
    • Tkinter Checkbutton
    • Tkinter Menu
    • Tkinter Menubutton
    • Tkinter OptionMenu
    • Tkinter Messagebox
    • Tkinter Grid
    • Tkinter Pack
    • Tkinter Scale
    • Tkinter Table
    • Python Tkinter Entry
    • Tkinter after
    • Tkinter Colors
    • Tkinter Font
    • Tkinter PhotoImage
    • Tkinter TreeView
    • Tkinter Notebook
    • Tkinter Combobox
    • Tkinter Bind
    • Tkinter Icon
    • Tkinter Window Size
    • Tkinter Color Chart
    • Tkinter Slider
    • Tkinter Calculator
    • Tkinter geometry
    • Tkinter image
    • Tkinter input box
    • Tkinter mainloop

Tkinter Spinbox

By Shobha ShivakumarShobha Shivakumar

Tkinter-Spinbox

Introduction to Tkinter Spinbox

Tkinter Spinbox widget is an alternative to the Entry widget. It can be used to replace ordinary entry where the user can choose from a limited number of values only. When there are a fixed number of values, and we want to select the values, we use a spinbox widget, a standard Tkinter entry widget variant. And it is very much like an ordinary widget except that it can specify the values to be allowed, which can be either range or a tuple. This spinbox widget is available only in the versions of python above 2.3.

Syntax of Tkinter Spinbox:

w = Spinbox(master, option,..)

where master represents the parent window, and option is the list of options that can be used for the widget, and options are the key-value pairs that are separated by commas.

Tkinter Spinbox Widget

The most commonly used list of options for the widget are:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • active background: This option represents the slider colour and the arrowheads colour when the mouse is placed over them.
  • bg: This option represents the slider colour and the arrowheads colour when the mouse is not placed over them.
  • bd: The trough is covered by three-d borders around the entire perimeter of the trough, and this option represents its width. It also represents the width of the three-d effect on arrowheads and slider. By default, the trough has no borders around the perimeter of the trough, and the arrowheads and slider have a two-pixel border.
  • command: This option calls the procedure whenever there is a movement of the scrollbar.
  • font: This represents the font to be used in the widget.
  • fg: This represents the color of the text in the spinbox.
  • cursor: When the mover is present over the scrollbar, this option appears.
  • disabledbackground: Whenever the widget is disabled, this option represents the background color to be used.
  • disabledforeground: Whenever the widget is disabled, this option represents the text color to be used.
  • format: This option represents the string format, and there is no default value for this.
  • from: This option represents the minimum value that can be used to limit the range in the spinbox.
  • justify: The default value for this option is LEFT.
  • repeatDelay: This option and repeat interval control the auto-repeat button, and both the values can be represented in milliseconds.
  • repeatInterval: This option and repeat delay control the auto-repeat button, and both the values can be represented in milliseconds.
  • state: This option can be either of NORMAL or DISABLED or read-only. The default value for this option is NORMAL.
  • textvariable: There is no default value for this option text variable.
  • to: This option represents the minimum value that can be used to limit the range in the spinbox.
  • validate: This option represents the mode of validation. The default value for this option is NONE.
  • validateCommand: This option represents the callback of validation. There is no default value for this option.
  • values: This option represents the valid values contained in the tuple for the widget. This option can override from, to and increment.
  • vcmd: This option represents the callback of validation. There is no default value for this option.
  • wrap: There is a wrapping of up and down buttons if the condition is true.
  • relief: SUNKEN is the default value for relief.
  • width: This option represents the width of the characters in the widget. Twenty is the default value.
  • xscrollcommand: The option allows the user to scroll the spinbox horizontally.

Methods of Tkinter Spinbox

There are several methods that can be implemented on spinbox objects, they are:

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,697 ratings)
  • Delete(startindex[,endindex]): When a specific character or a range of characters must be deleted, we used this method.
  • get(startindex[,endindex]): When a specific character or a range of characters must be returned, we used this method.
  • identify(x,y): Given the location, the widget element can be identified using this method.
  • index(index): Depending on the index given, the index’s absolute value is returned using this method.
  • insert(index,[,string]..): The specified location of the index is where the string is inserted by using this method.
  • invoke(element): The button of the spinbox is invoked using this method.
  • selection_clear(): This method helps in clearing the selection.
  • selection_get(): The selected text is returned using this method. This method will raise an exception if there is no selection currently.
  • selection(‘range’, start, end): The text between the indices representing the start and end values is selected using this method.
  • selection(‘to’, index): The text between the given index and the selection anchor is selected by using this method.
  • selection(‘from’, index): The given index specifies the position at which the selection anchor must be pointed at. The selection anchor is initially set to zero.

Example of Tkinter Spinbox

Below are the examples of Tkinter Spinbox:

Python program using Tkinter Spinbox to select from a list of numbers from one to sixty incremented by one.

Code:

#Import tkinter package
import tkinter as tk
#Define a window name
root1 = tk.Tk()
text_variable = tk.DoubleVar()
#declare the spinbox widget by assigning values to from_, to and increment
spin_box = tk.Spinbox(
    root1,
    from_=1.0,
    to=50.0,
    increment=1.0,
    textvariable=text_variable
)
#To show the content in the window
spin_box.pack()
root1.mainloop()

Output:

Tkinter Spinbox Example

In the above code, we first import the Tkinter packages and libraries and then define a window by giving it a name. In our example, we call it tk. In our example, the values that we are trying to keep fixed must be floating-point numbers or double; hence we declare a variable and assign it to double. We then define the spinbox widget with from_, to values. In our example, we are allowing the user to select from a range of values 1.0 to 50.0, which gets incremented by 1 every time the user clicks on the upward arrowhead and the same is shown in the output snapshot above. The pack function is used to display the content in the window.

Conclusion

This article has learned about the Tkinter spinbox, the syntax attributes, methods, and examples of Tkinter spinbox. It can be used to create our own GUI’s along with other widgets provided by Tkinter.

Recommended Articles

This is a guide to Tkinter Spinbox. Here we discuss a brief overview of Tkinter Spinbox Widget, Methods, and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –

  1. Tkinter Listbox
  2. Tkinter Checkbutton
  3. Tkinter Text
  4. Tkinter Grid
Popular Course in this category
Data Science with Python Training (24 Courses, 14+ Projects)
  24 Online Courses |  14 Hands-on Projects |  110+ Hours |  Verifiable Certificate of Completion
4.8
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more