我希望能够动态调整下面的浮点数字微调框(spin1
)的增量。为此,我创建了第二个自旋框(spin2
),它的值(0.1, 0.01, 0.001, 0.0001)
,决定了spin1
的步长
只要我总是从较粗的增量移动到较细的增量,这就可以很好地工作。当我尝试从更细的增量移动到更粗的增量时,它失败了,因为该值被截断到增量的精度
使用以下代码,可以重现问题:
我希望在spin1
中看到值0.11
,但我看到的是0.1
。为什么会发生这种情况,我如何修复它
from tkinter import Tk, Spinbox
def update():
spin1['increment'] = float(spin2.get())
window = Tk()
spin1 = Spinbox(window, from_=0.0, to=10.0, increment=0.1)
spin2 = Spinbox(window, values=(0.1, 0.01, 0.001, 0.0001), command=update)
spin1.pack()
spin2.pack()
window.mainloop()
补充意见:
spin1
设置为2.6
,最终结果仍然是0.1
,而不是0.2
李>
您可以将
format="%.4f"
(基于spin2
中的0.0001
的最大小数点)添加到spin1
:相关问题 更多 >
编程相关推荐