如何根据Python中另一个tkinter“Scale”小部件的值来控制tkinter“Scale”小部件的值?

时间:2015-07-13 11:35:58

标签: python tkinter widget ttk tkinter-scale

我有3个Scale小部件scale1scale2scale3。我想使用scale1以下列方式控制scale2scale3的值:

  • 我不希望scale2scale3scale1具有相同的价值。
  • 如果scale1的值为1,则scale2的值为2.5,scale3的值为1.5
  • 当我向右移动scale1以将其值增加到1.05时,scale2的值将为2.55而scale3将为1.55
  • 或者我向左移动scale1以将其值减少到0.95,然后scale2值将为2.45而scale3值将为1.45

这是我的代码:

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 个答案:

答案 0 :(得分:1)

这就是我所做的:

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