当`tk.Scale`的值发生变化时,如何自动更新`tk.Label`?

时间:2015-03-01 23:27:01

标签: python user-interface tkinter tkinter-scale

我放入了我的框架比例,但我不确定如何在我的标签中显示此比例的值。我需要在每次比例移动时更新它。我怎样才能做到这一点?

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)

1 个答案:

答案 0 :(得分:6)

如果比例和标签共享一个公共变量,标签将自动更新。您可以调用变量的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()