如何在 `tk.Scale` 的值发生变化时自动更新 `tk.Label`?
How to automatically update `tk.Label` when the value of `tk.Scale` changes?
我输入了我的框架比例尺,但我不确定如何在我的标签中显示这个比例尺的值。每次秤移动时我都需要更新它。我怎样才能做到这一点?
self.options_settings.framepripojeni6 = Frame(self.options_settings.tab1)
self.options_settings.framepripojeni6.pack(side=tkinter.TOP, expand=0, fill=tkinter.BOTH, padx=2, pady=4)
self.options_settings.scale = Scale(self.options_settings.framepripojeni6,from_=1, to=60, length=350)
self.options_settings.scale.pack(side=tkinter.TOP)
self.options_settings.labelScale = tkinter.Label(self.options_settings.framepripojeni5, text="x")
self.options_settings.labelScale.pack(side=tkinter.LEFT)
如果比例尺和标签共享一个公共变量,标签将自动更新。您可以调用变量的 set
方法来提供比例的默认值。
这是一个简单的例子:
import tkinter as tk
root = tk.Tk()
scalevar = tk.IntVar()
scalevar.set(50)
scale = tk.Scale(root, from_=0, to=100,
variable=scalevar, orient="horizontal")
label = tk.Label(root, textvariable=scalevar)
scale.pack(side="top", fill="x", expand=True)
label.pack(side="top", fill="x", expand=True)
root.mainloop()
我输入了我的框架比例尺,但我不确定如何在我的标签中显示这个比例尺的值。每次秤移动时我都需要更新它。我怎样才能做到这一点?
self.options_settings.framepripojeni6 = Frame(self.options_settings.tab1)
self.options_settings.framepripojeni6.pack(side=tkinter.TOP, expand=0, fill=tkinter.BOTH, padx=2, pady=4)
self.options_settings.scale = Scale(self.options_settings.framepripojeni6,from_=1, to=60, length=350)
self.options_settings.scale.pack(side=tkinter.TOP)
self.options_settings.labelScale = tkinter.Label(self.options_settings.framepripojeni5, text="x")
self.options_settings.labelScale.pack(side=tkinter.LEFT)
如果比例尺和标签共享一个公共变量,标签将自动更新。您可以调用变量的 set
方法来提供比例的默认值。
这是一个简单的例子:
import tkinter as tk
root = tk.Tk()
scalevar = tk.IntVar()
scalevar.set(50)
scale = tk.Scale(root, from_=0, to=100,
variable=scalevar, orient="horizontal")
label = tk.Label(root, textvariable=scalevar)
scale.pack(side="top", fill="x", expand=True)
label.pack(side="top", fill="x", expand=True)
root.mainloop()