1

I have 3 Scale widgets scale1, scale2 and scale3. I would like to use scale1 to control the values of scale2 and scale3 in the following manner:

  • I don't want scale2 and scale3 having the same value as scale1.
  • If value of scale1 is 1, value of scale2 is 2.5 and value of scale3 is 1.5
  • When I move scale1 to the right to increase its value to 1.05, the value of scale2 will be 2.55 and scale3 will be 1.55
  • Or I move scale1 to the left to decrease its value to 0.95, then scale2 value will be 2.45 and scale3 value will be 1.45

Here is my code:

from tkinter import *
from tkinter import ttk

class Init(object):
    def __init__(self, master):
        self.master = master
        self.master.title('Scale demo')
        self.master.configure(background='#ededed')
        self.scale_1_var = DoubleVar()
        self.scale_1_var.set(1)
        self.scale_2_var = DoubleVar()
        self.scale_2_var.set(1)
        self.scale_3_var = DoubleVar()
        self.scale_3_var.set(1)

class Scale(Init):
    def __init__(self, master):
        Init.__init__(self, master=master)

        self.frame_master = ttk.Frame(self.master, padding=(10,10,10,10))
        self.frame_master.pack(fill=BOTH)

        self.label_1 = ttk.Label(self.frame_master, text='Scale 1:')
        self.label_1.grid(row=0, column=1, sticky='w')
        self.scale_1 = ttk.Scale(self.frame_master, orient=HORIZONTAL, length=400, from_=0, to=4, variable=self.scale_1_var)
        self.scale_1.grid(row=0, column=2, sticky='we')
        self.entry_1 = ttk.Entry(self.frame_master, textvariable=self.scale_1_var, width=3)
        self.entry_1.grid(row=0, column=3, sticky='e')

        self.label_2 = ttk.Label(self.frame_master, text='Scale 2:')
        self.label_2.grid(row=1, column=1, sticky='w')
        self.scale_2 = ttk.Scale(self.frame_master, orient=HORIZONTAL, length=400, from_=0, to=4, variable=self.scale_2_var)
        self.scale_2.grid(row=1, column=2, sticky='we')
        self.entry_2 = ttk.Entry(self.frame_master, textvariable=self.scale_2_var, width=3)
        self.entry_2.grid(row=1, column=3, sticky='e')

        self.label_3 = ttk.Label(self.frame_master, text='Scale 3:')
        self.label_3.grid(row=2, column=1, sticky='w')
        self.scale_3 = ttk.Scale(self.frame_master, orient=HORIZONTAL, length=400, from_=0, to=4, variable=self.scale_3_var)
        self.scale_3.grid(row=2, column=2, sticky='we')
        self.entry_3 = ttk.Entry(self.frame_master, textvariable=self.scale_3_var, width=3)
        self.entry_3.grid(row=2, column=3, sticky='e')

        self.scale_1.config(command=self.sc1)

    def sc1(self, *argv):
        self.a = self.scale_1.get()
        self.scale_2.set(self.a)
        self.scale_3.set(self.a)

root=Tk()
Scale(root)
root.mainloop()

1 Answer 1

1

here is what i've done:

def sc1(self, *argv):
        self.a = float(argv[0]) 
        # rather than re-request the value resulting in more function calls
        # i'm using the value that is passed in during the callback
        if not hasattr(self, "a_prev"): # this is a hacky method to 
            self.a_prev = 1             # remember the previous value

        b_diff = self.scale_2.get() - self.a_prev 
        c_diff = self.scale_3.get() - self.a_prev
        # calculate the differences to the previous value

        self.scale_2.set(self.a+b_diff) # set the scales
        self.scale_3.set(self.a+c_diff)
        self.a_prev = self.a # remember current value as the new previous
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.