MuffinJets
MuffinJets

Reputation: 1

Why isn't the variable from my spinbox returning? (python, tkinter)

import tkinter as tk
from tkinter import *

root = tk.Tk()
root.grid()

numEntry = tk.StringVar()
printButton = tk.IntVar()


# Check if the number submitted is zero.
def checkZero():
    num = numEntry.get()
    print("Number Inputted =",num)
    if num == 0:
        print("Yes")
    elif num != 0:
        print("No")



numEntry = Spinbox(root, from_= 0, to = 100000, wrap=True)
printButton = Button(root, text="Print", command=checkZero)

numEntry.grid(column=0, row=0)
printButton.grid(column=1, row=0)

root.mainloop()

What am I doing wrong here?

It's as if the .get() method isn't being called at all.

Very new to programming and python, so there's a solid chance I'm missing something small.

Upvotes: 0

Views: 36

Answers (1)

MuffinJets
MuffinJets

Reputation: 1

I found out what the issue was!

I didn't make sure that my numEntry turned out to be an integer.

import tkinter as tk
from tkinter import *

root = tk.Tk()
root.grid()

numEntry = tk.StringVar()
printButton = tk.IntVar()


# Check if the number submitted is zero.
def checkZero():
    num = int(numEntry.get())
    print("Number Inputted =",num)
    if num == 0:
        print("Yes")
    elif num != 0:
        print("No")



numEntry = Spinbox(root, from_= 0, to = 100000, wrap=True)
printButton = Button(root, text="Print", command=checkZero)

numEntry.grid(column=0, row=0)
printButton.grid(column=1, row=0)

root.mainloop()

Upvotes: 0

Related Questions