PHD12
PHD12

Reputation: 1

Tkinter Scale Widget -- Continuous Operation

Newbie question: How to make a tkinter scale operate continuously so that whenever the value is changed, the new value is written (to I2C or to a file) and this repeats until the user terminates operation of the overall program (there will need to be multiple scale controls that are operational on the GUI until the program is exited by user). The purpose is to keep sending volume values as changed by the slider. I've had no problem with buttons, text entry, radiobuttons, but I just don't see what I need to add to accomplish this. Here is code:

from tkinter import *  # Python 3
from math import log10

root = Tk()
root.title("Fidelity Science Audio Control GUI")
root.geometry("500x500")
Volume = IntVar
Vol = Scale(root, variable=Volume, from_=0, to=100, tickinterval=10, orient=VERTICAL,
       length=400, width=20, borderwidth=10, background="light blue", foreground="black",
       troughcolor="light blue", label="Volume")
Vol.set(50)
Vol.grid(row=10, column=1)
if Vol.get() > 0:
    Volume = Vol.get()
    LogVolume = log10(Volume)
    print("Volume = ", Volume)
    print("Value sent To I2C =", LogVolume)

root.mainloop()

Do I need to use a loop function like while or if? Thanks!

Upvotes: 0

Views: 434

Answers (1)

acw1668
acw1668

Reputation: 46751

You can use the callback associated to the Scale via command option to send the volume to I2C when the value of the Scale is changed:

from tkinter import *  # Python 3
from math import log10

def on_volume_change(volume):
    if volume > 0:
        LogVolume = log10(volume)
        print("Volume =", volume)
        print("Value sent to I2C =", LogVolume)
        # send the volume to I2C

root = Tk()
root.title("Fidelity Science Audio Control GUI")
root.geometry("500x500")

Volume = IntVar(value=50)
Vol = Scale(root, variable=Volume, from_=0, to=100, tickinterval=10, orient=VERTICAL,
            length=400, width=20, borderwidth=10, background="light blue", foreground="black",
            troughcolor="light blue", label="Volume", command=on_volume_change)
Vol.grid(row=10, column=1)

# send the initial volume
on_volume_change(Volume.get())

root.mainloop()

Upvotes: 1

Related Questions