简体   繁体   English

tkinter 缩放小部件的未知选项

[英]Unknow option for tkinter scale widget

I am coding a GUI where you have to enter a number.我正在编写一个 GUI,您必须在其中输入一个数字。 I want to have a scale but it fits my purpose very well.我想要一个体重秤,但它非常符合我的目的。 The problem is a scale goes to 1e-9 precision.问题是比例达到 1e-9 精度。 This is much to precise for me.这对我来说非常准确。 I am using ttk extension for tkinter .我使用ttk延期tkinter I have tried a couple of things but I saw that there is a digits option for the scale which, when there is a StringVar() , leaves the number of digits but it doesn't work, it cannot recognise the option.我尝试了几件事,但我看到比例尺有一个数字选项,当有一个StringVar() ,会留下数字但不起作用,它无法识别该选项。 I tried to look in the library but I couldn't find anything, it was too complicated.我试着去图书馆看,但我找不到任何东西,它太复杂了。

Here is how my code is formed:这是我的代码的形成方式:

scaleValue = StringVar()
scale = ttk.Scale(content, orient=HORIZONTAL, from_=1, to=5, variable=scaleValue, length=150, digits=1)  # here digits ins't recongnised
scale.grid(column=x, row=y)

scaleValueLabel = ttk.Label(content, textvariable=scaleValue)
scaleValueLabel.grid(column=x', row=y')

Here is the documentation:这是文档:

An integer specifying how many significant digits should be retained when converting the value of the scale to a string.一个整数,指定在将刻度值转换为字符串时应保留多少有效数字。 Official Documentation : http://www.tcl.tk/man/tcl8.6/TkCmd/scale.htm官方文档:http://www.tcl.tk/man/tcl8.6/TkCmd/scale.htm

digits is a parameter only available to tk.Scale . digits是一个仅适用于tk.Scale的参数。 If you switch to using it, then your code will work:如果您切换到使用它,那么您的代码将起作用:

scale = tk.Scale(root, orient=tk.HORIZONTAL, from_=1, to=5, variable=scaleValue, length=150, digits=5)

But if you really want to use ttk.Scale , you can use a different approach.但是如果你真的想使用ttk.Scale ,你可以使用不同的方法。 Instead of using a textvariable in your Label , you can trace the changes on your variable, process the value first, and then pass back to your Label .而不是使用的textvariable在你的Label ,你可以跟踪你的变量的变化,首先处理的值,然后传回给你的Label

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

scaleValue = tk.DoubleVar()
scale = ttk.Scale(root, orient=tk.HORIZONTAL, from_=1, to=5, variable=scaleValue, length=150)  # here digits ins't recongnised
scale.grid(column=0, row=0)

scaleValueLabel = tk.Label(root, text="0")
scaleValueLabel.grid(column=0, row=1)

def set_digit(*args):
    scaleValueLabel.config(text="{0:.2f}".format(scaleValue.get()))

scaleValue.trace("w", set_digit)

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM