gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:33 27 4
gpt4 key购买 nike

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

  • 我不希望 scale2scale3 具有与 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

关于python - 如何根据Python中另一个 tkinter `Scale` 小部件的值来控制 tkinter `Scale` 小部件的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31382326/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com