【问题标题】:How to automatically update `tk.Label` when the value of `tk.Scale` changes?当`tk.Scale`的值改变时如何自动更新`tk.Label`?
【发布时间】:2015-05-02 06:11:41
【问题描述】:

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

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)

【问题讨论】:

    标签: python user-interface tkinter tkinter-scale


    【解决方案1】:

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

    【讨论】:

    • 是的,谢谢,在 python 3.4 中我必须使用带有小 T 的 tkinter
    猜你喜欢
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多