Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter - Getting values from spinbox

Tags:

python

tkinter

I'm currently having trouble getting the correct value from the spinbox widget. I do not know what is wrong. I have searched for solutions and have come empty. What am I doing wrong? Here is my code:

from Tkinter import *

#create Tk window
root = Tk()

#set name of window
root.title('Testing Values')

#initalise values from user (spinbox values)
item_1 = IntVar()
a = item_1.get()

def print_item_values():
    global a
    print a


#item 1 spinbox
item_1 = Spinbox(root, from_= 0, to = 10, width = 5)
item_1.grid(row = 0, column = 0)

#print values
value_button = Button(root, text = 'Print values', width = 10, command = print_item_values)
value_button.grid(row = 0, column = 1)


root.mainloop()
like image 730
newang Avatar asked May 08 '17 13:05

newang


People also ask

How do I get values from Spinbox?

A Spinbox has an area for showing the current value and a pair of arrowheads. When you click the upward-pointing arrowhead, the Spinbox advances the current value to the next higher value in the sequence. If the current value reaches the maximum value, you can set it to the minimum value.

What is Spinbox in tkinter?

The Spinbox widget is an alternative to the Entry widget. It provides the range of values to the user, out of which, the user can select the one. It is used in the case where a user is given some fixed number of values to choose from.

How to set default value in Spinbox?

We can set the default value for the spinbox widget by defining the Value using StringVar() object. The default value plays a vital role for any widget, as it helps you to define the bounded value.

How to disable Spinbox in Tkinter?

Enable disable Spinbox To disable we can use option state or use config().


2 Answers

In your code, a is never updated. Instead, to get the spinbox value, just use its .get() method:

item_1 = Spinbox(root, from_= 0, to = 10, width = 5)
item_1.grid(row = 0, column = 0)

def print_item_values():
    print item_1.get()

Tkinter Spinbox Documentation

like image 194
Josselin Avatar answered Sep 19 '22 17:09

Josselin


You can make following changes in your code

from tkinter import *

root = Tk()

root.title('Testing Values')

item_1 = IntVar()

def print_item_values():
    a = item_1.get()
    print(a)

item_1 = Spinbox(root, from_= 0, to = 10, width = 5)
item_1.grid(row = 0, column = 0)

value_button = Button(root, text = 'Print values', width = 10, command = 
print_item_values)
value_button.grid(row = 0, column = 1)

root.mainloop()
like image 25
rutvik mehta Avatar answered Sep 18 '22 17:09

rutvik mehta