[英]How control the value of a tkinter `Scale` widget depending on the value of another tkinter `Scale` widget in Python?
I have 3 Scale
widgets scale1
, scale2
and scale3
. 我有3个Scale
小部件scale1
, scale2
和scale3
。 I would like to use scale1
to control the values of scale2
and scale3
in the following manner: 我想以以下方式使用scale1
来控制scale2
和scale3
的值:
scale2
and scale3
having the same value as scale1
. 我不想scale2
和scale3
具有相同的值scale1
。 scale1
is 1, value of scale2 is 2.5 and value of scale3
is 1.5 如果scale1
值为1,scale2的值为2.5, scale3
值为1.5 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 当我向右移动scale1
以将其值增加到1.05时, scale2
的值为2.55, scale3
为1.55 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 或者我将scale1
移到左侧以将其值减小到0.95,然后scale2
值为2.45, scale3
值为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()
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.