0

Good Morning/Evening,

I want to read a number from a spinbox, and if it is 2, it should print something. But my code does not work out. I've tried it with a slider instead of a spinbox and it worked out. But for me, it is really important to use a spinbox, so I hope somebody have an idea.

Code:

from tkinter import *
    def a():
      if spin.get()==2:
        print("Hello World")
root = Tk()
root.geometry('300x100')
spin =Spinbox(root, from_=0, to=10,command=a)
button = Button(root, text='Enter')
button.pack(side=RIGHT)
spin.pack(side=RIGHT)
root.mainloop()
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
xme
  • 1

1 Answers1

0

Adding to @coolCloud's answer I would suggest setting a textvariable for spinBox. So if the user changes it using the entry. It would automatically be updated.

Something like this:

from tkinter import *

def a(*event):
    if text.get()=='2':
        print("Hello World")

root = Tk()
root.geometry('300x100')

text = StringVar()
text.trace('w', a) # or give command=a in the button if you want it to call the event handler only when the button is pressed

spin =Spinbox(root, from_=0, to=10, textvariable=text)
button = Button(root, text='Enter')

button.pack(side=RIGHT)
spin.pack(side=RIGHT)

root.mainloop()
JacksonPro
  • 3,135
  • 2
  • 6
  • 29