python - 如何让 tkinter scale 小部件将其上限设置为更新的变量?

标签 python python-3.x tkinter

我正在尝试在 tkinter scale 小部件上设置“to=”配置选项。我想从条目小部件获取用户输入并将其用作比例的上限。

按照我现在的设置方式,当用户在输入框中输入一个值并单击按钮后,比例会停留在 0。在用户更改变量后,“to=”配置选项似乎没有更新。

如何让比例小部件将其上限设置为新值?

代码如下:

import tkinter

class HomePage:
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.geometry('300x300')

        self.entry_value = tkinter.IntVar()

        self.entry_box = tkinter.Entry(master=self.root)
        self.entry_box.pack(pady=10)

        self.button = tkinter.Button(master=self.root, text='get value', command=self.set_value)

        self.button.pack(pady=10)

        self.scale = tkinter.Scale(master=self.root, from_=0, to=self.entry_value.get(), orient='horizontal')
        self.scale.pack(pady=10)

        self.show_entry_value = tkinter.Button(self.root, text='Updated Value',
                                               command=lambda: print(self.entry_value.get()))
        self.show_entry_value.pack()

    def set_value(self):
        self.entry_value.set(self.entry_box.get())


app = HomePage()
app.root.mainloop()

最佳答案

使用configure方法改变值:

self.scale.configure(to=the_new_value)

关于python - 如何让 tkinter scale 小部件将其上限设置为更新的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40589834/

相关文章:

python - 尝试将 dict 写入文件时出现 KeyError [Python]

python - 如何删除 Python 中的 ASP 仅注释 block (在 Sublime Text 2 上)?

python - 为什么bokeh应用回调会转换变量类型? (ColumnDataSource 到 pandas df)

python - Tkinter 获取图像以跟随光标并进行先前的移动

python - scikit-learn 中的 "fit"方法有什么作用?

python - 获取列中项目出现的频率(以百分比形式表示)

python - 将此 while 循环转换为更高效的 while 循环

python - Pygame 的当前 GUI 选项

python - 图片 "pyimage5"不存在添加5行代码后出错

python-3.x - 尝试在 Windows 10 高 DPI 显示器上解决模糊的 tkinter 文本 + 缩放,但担心我的方法不是 Pythonic 或不安全