Monte Carlo
Monte Carlo

Reputation: 457

In Tkinter how can I constantly get a slider value

I've just started making a GUI interface for a robotics project I'm working on, and I've run aground. I want my slider in my Tkinter widget to print the current position/value that it has whenever it is adjusted. Right now, the only way to constantly get input is to manually press a button that pulls that info for me. I thought the way I could get this data would be to run Throttle.get() after I run that main loop, but that only gets executed until I close my widget. I'm fairly new to Tk but here's my script so far.

from Tkinter import *
master = Tk()

def getThrottle(): # << I don't want to use a button, but I am in this case.
    print Throttle.get()

Throttle = Scale(master, from_=0, to=100, orient=HORIZONTAL)
Throttle.set(0)
Throttle.pack()

getB = Button(master, text ="Hello", command = getThrottle)  
getB.pack()

mainloop()

Upvotes: 2

Views: 4632

Answers (1)

user2555451
user2555451

Reputation:

That can be done by simply setting the command option of the scale:

from Tkinter import *
master = Tk()

def getThrottle(event):
    print Throttle.get()

Throttle = Scale(master, from_=0, to=100, orient=HORIZONTAL, command=getThrottle)
Throttle.set(0)
Throttle.pack()

mainloop()

Now, as you move the scale, data is printed in the terminal in real time (no button presses needed).

Upvotes: 3

Related Questions