Header Ads

Header ADS

Spinbox in tkinter

Tkinter : Spinbox widget 

The spinbox widget is a alternative to the Entry widget .
It provides the range of values to the user , out of which the user can select the one or even Write it own value to it .



So in this Blog we will discuss how to create a spinbox , and also work with its some attributes .


So let's code :

# tkinter spinbox : Dynamic coding
# creating spin box

from tkinter import *

root = Tk()
root.title('Dynamic Coding')

spin = Spinbox(root,from_ = 0 ,to = 10,width=5)

spin.pack(pady=10)

root.mainloop()

OUTPUT :

The Spinbox class is used to create a spin box , the first argument of tkinter widget is where to place that widget in our case we want to place Spinbox on root window so i pass root window .
the second option is from_  and to these options specifies from where to start inserting spinbox  value and where it will end .
The spinbox have two butons on right side the top button increase the value or  you can say increase the index and bottom button do just opposite of it .
Width  option justifies how many numbers of character will show at a time in spinbox .









Add stings list in spinbox :

# tkinter spinbox : Dynamic coding

from tkinter import *

root = Tk()
root.title('Dynamic Coding')

data = ['Python','C++','java','Php']
spin = Spinbox(root,values=data,width=10)
spin.pack(pady=10)

root.mainloop()

At this time we use a option values and in it we pass a list of some values. 

 OUTPUT :





set and get value :

# tkinter spinbox : Dynamic coding
# set and get a value .
from tkinter import *

def func():
	print(" selected value is : ", spin.get())

	# .. function ends .

root = Tk()
root.title('Dynamic Coding')

# .. list of data ..

data = ['Python','C++','java','Php']

var = StringVar()

spin = Spinbox(root,values=data,width=10,textvariable = var)
var.set(data[2])

spin.pack(pady=10)

btn = Button(root,text='press here ',command=func)
btn.pack(pady=10)


root.mainloop()

In this example code we use a Textvariable for changing the  value of spinbox but we use the spinbox name to fetch its data at that place you can also Textvariable .

 OUTPUT :



  






Spinbox attributes :

  • active background: This option represents the slider color and the arrowheads color when the mouse is placed over them.
  • bg: This option represents the slider color and the arrowheads color 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 its width is represented by this option. 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: The button called auto-repeat is controlled by this option along with repeat interval and both the values can be represented in milliseconds.
  • repeatInterval: The button called auto-repeat is controlled by this option along with repeat delay 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 horizontally scroll the spinbox.
  • active background: This option represents the slider color and the arrowheads color when the mouse is placed over them.
  • bg: This option represents the slider color and the arrowheads color 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 its width is represented by this option. 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: The button called auto-repeat is controlled by this option along with repeat interval and both the values can be represented in milliseconds.
  • repeatInterval: The button called auto-repeat is controlled by this option along with repeat delay 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 / 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.
Methods of Spinbox :
    • 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 absolute value of the index 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.


    More tkinter related Article :





    This blog is contributed by Yogesh singh .
    Please leave a feedback in comment section

     


    No comments

    Powered by Blogger.