I am expecting my button function to print a number (minutes_selected) based on the current value of the Spinner UI element, but the variable is always PY_VAR1:
from tkinter import *
from tkinter import ttk
def get_minutes():
global minutes_selected
minutes_selected.get()
print(mintes_selected)
root = Tk()
minutes_selected = StringVar()
minutes_spinbox = Spinbox(root, from_ = 1, to = 1440, textvariable = minutes_selected).pack()
Is there some basic misunderstanding with regards to how variables can be access from within a function?
解决方案
You need to use the get method to get the value of the variable.
Change this:
print(mintes_selected)
To this:
print(minutes_selected.get())