I'm trying to create a GUI to run an experiment using tkinter. I need to assign the value selected by the user on a Scale widget to a variable called 'amount'. However, scale.get() always assigns the value '0' to amount, regardless of where I am on the Scale.
Most answers about the Scale widget seem to suggest that scale.get() works perfectly for everyone else, so I am not sure what I'm doing wrong. I'm using Python 3 and I wrote the following code to test just the get method:
import tkinter as tk
root = tk.Tk()
root.title("Test")
slider = tk.Scale(orient='horizontal', label = 'Amount', length = 500, from_= 0, to = 1000, bg = 'white', fg = 'black', sliderlength = 20)
amount = slider.get()
slider.pack()
print (amount)
root.mainloop()
According to other answers, amount
should be equal to whatever value I choose on slider
. But the print command prints 0
.
This is my code and the slider
.
Copy comment: I tried the following:
def get_value(var): amount = var.get() return amount var = tk.DoubleVar() scale = tk.Scale( root, variable = var ) scale.pack(anchor=CENTER) print (get_value(var))