0

I combined this answer & this answer to come up with a script that outputs a different value every time a slider in tkinter is adjusted. It works but prints all of the values for the array and ideally only want to see the changed value. Could someone point me in the right direction?

import Tkinter as tk

scales=list()
Nscales=10

class App:
    def __init__(self):
    self.root = tk.Tk()
    for i in range(Nscales):
        self.slider = tk.Scale(self.root, from_=1, to=5, orient=tk.HORIZONTAL) # creates widget
        self.slider.bind("<ButtonRelease-1>", self.updateValue)
        self.slider.pack() 
        self.slider.set(3) 
        scales.append(self.slider) # stores widget in scales list
    self.root.mainloop()
def updateValue(self, event):
    for i in range(Nscales):
        print ("Scale %d has value %d" %(i,scales[i].get()))

app=App()
nsl
  • 1
  • Possible duplicate of [Getting the widget that triggered an Event?](https://stackoverflow.com/questions/4299145/getting-the-widget-that-triggered-an-event) – Aran-Fey Apr 16 '18 at 18:07
  • 1
    Instead of iterating over all the scales in your list, just print the value of `event.widget`. – Aran-Fey Apr 16 '18 at 18:08

1 Answers1

0

This would be an ideal place to make your own Scale widget. You could assign the numbers manually or use a class variable to keep track of them:

import Tkinter as tk

Nscales=10

class NslScale(tk.Scale):
    '''A custom Scale that prints its value on mouserelease'''
    count = 0
    def __init__(self, master=None, **kwargs):
        tk.Scale.__init__(self, master, **kwargs)
        self.num = self.count
        NslScale.count += 1
        self.bind("<ButtonRelease-1>", self.updateValue)
    def updateValue(self, event):
        print("Scale %d has value %d" %(self.num, self.get()))

root = tk.Tk()
for i in range(Nscales):
    slider = NslScale(root, from_=1, to=5, orient=tk.HORIZONTAL) # creates widget
    slider.pack()
    slider.set(3)
root.mainloop()

Also, your App class adds no value. Get rid of that.

Novel
  • 13,406
  • 2
  • 25
  • 41