更改一个比例小部件将更改第二个

Changing one scale widget will change the secone one

我的代码中有两个比例小部件,它们应该显示列表中的值。我希望能够为两个滑块设置单独的值。例如,对于文件 1“2.4”和文件 2“5.025”。但目前我只能更新第二个(下部)滑块而不会触发第一个(上部)滑块。如果我用上一个,第二个的值也变了。

我一直在执行 lambda 函数,所以如果有人能给我提示,我会创建它。 谢谢!

import tkinter as tk


class File_Selection():
    def __init__(self, frame, text):

        self.frame = frame
        self.text = text

        self.label_file = tk.Label(self.frame, text=text)
        self.label_file.pack(side="left", anchor="s")

        self.label_test = tk.Label(self.frame, text="| Select value: ")
        self.label_test.pack(side="left", anchor="s")

        self.scale = tk.Scale(self.frame, orient=tk.HORIZONTAL, from_=0)
        self.scale.pack(side="left", anchor="s")

class View:

    def __init__(self, view):

        self.view = view

        self.frame = tk.Frame(self.view)
        self.frame.pack()
    
        self.frame_row1 = tk.Frame(self.frame)
        self.frame_row1.pack(side="top")

        self.frame_row2 = tk.Frame(self.frame)
        self.frame_row2.pack(side="top")

        self.file_one = File_Selection(self.frame_row1, "File 1")
        self.file_two = File_Selection(self.frame_row2, "File 2")


class Controller:

    def __init__(self):
        self.root = tk.Tk()
        self.view = View(self.root)

        self.values = [1.01,2.4,3.6,4.89,5.025,6.547]

        self.view.file_one.scale.bind('<Enter>', self.update_scale)
        self.view.file_two.scale.bind('<Enter>', self.update_scale)

    def run(self):
        self.root.mainloop()

    def update_scale(self, event):
        self.active_scales = [self.view.file_one.scale, self.view.file_two.scale]

        for scale in self.active_scales:
            scale.config(from_=min(self.values), to=max(self.values), resolution=0.001, command=lambda value=scale: self.set_scale(value, scale))
            
    def set_scale(self, value, scale):
        self.newvalue = min(self.values, key=lambda x: abs(x-float(value)))
        scale.set(self.newvalue)

if __name__ == "__main__":
    c = Controller()
    c.run()

问题是您将两个比例都设置为 active_scales。您可以利用 event 并取消 active_scales.

您可以通过 event.widget 获得与 event 关联的 widget。因此,您可以将 update_scales 函数从

更改为
def update_scale(self, event):
    self.active_scales = [self.view.file_one.scale, self.view.file_two.scale]
        for scale in self.active_scales:
            scale.config(from_=min(self.values), to=max(self.values), resolution=0.001, command=lambda value=scale: self.set_scale(value, scale))

def update_scale(self, event):
    event.widget.config(from_=min(self.values), to=max(self.values), resolution=0.001, command=lambda value=event.widget: self.set_scale(value, event.widget))

此外,由于 self.values 似乎没有变化,因此似乎没有必要 config 每个比例尺更改每个 event 的边界和分辨率。相反,您可以将其从事件中拉出来并按如下方式使您 Controller class。

class Controller:

    def __init__(self):
        self.root = tk.Tk()
        self.view = View(self.root)

        self.values = [1.01,2.4,3.6,4.89,5.025,6.547]

        self.view.file_one.scale.bind('<Enter>', self.update_scale)
        self.view.file_two.scale.bind('<Enter>', self.update_scale)
        self.view.file_one.scale.config(from_=min(self.values), to=max(self.values), resolution=0.001)
        self.view.file_two.scale.config(from_=min(self.values), to=max(self.values), resolution=0.001)

    def run(self):
        self.root.mainloop()

    def update_scale(self, event):
        event.widget.config(command=lambda value=event.widget: self.set_scale(value, event.widget))
       
    def set_scale(self, value, scale):
        self.newvalue = min(self.values, key=lambda x: abs(x-float(value)))
        scale.set(self.newvalue)