如何根据 Python 中另一个 tkinter `Scale` 小部件的值来控制 tkinter `Scale` 小部件的值?
How control the value of a tkinter `Scale` widget depending on the value of another tkinter `Scale` widget in Python?
我有 3 个 Scale
小部件 scale1
、scale2
和 scale3
。我想使用 scale1
以下列方式控制 scale2
和 scale3
的值:
- 我不希望
scale2
和 scale3
具有与 scale1
相同的值。
- 如果
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()
这是我所做的:
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
我有 3 个 Scale
小部件 scale1
、scale2
和 scale3
。我想使用 scale1
以下列方式控制 scale2
和 scale3
的值:
- 我不希望
scale2
和scale3
具有与scale1
相同的值。 - 如果
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()
这是我所做的:
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