Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter:控制ttk.Scale和#x27;s增量与tk.Scale和tk.DoubleVar一样_Python_Tkinter - Fatal编程技术网

Python Tkinter:控制ttk.Scale和#x27;s增量与tk.Scale和tk.DoubleVar一样

Python Tkinter:控制ttk.Scale和#x27;s增量与tk.Scale和tk.DoubleVar一样,python,tkinter,Python,Tkinter,我曾经使用tk.Scale的digits属性来确保标签或Spinbox中的数字在滑块移动时显示固定的小数位数。比如3.4564444.5675555.678 然而,随着ttk.Scale数字和分辨率的消失。如果将tk.DoubleVar用作刻度变量,则留下一个很长的十进制数。因此,根据这篇文章: 我必须为标签编写我自己的数字显示方案,当我已经使用一个widget类时,这看起来很疯狂。我还没有看到如何保持DoubleVar,因为我需要更改它的字符串表示形式 有没有更简单的方法来实现我想要的 更新

我曾经使用
tk.Scale
digits
属性来确保
标签
Spinbox
中的数字在滑块移动时显示固定的小数位数。比如3.4564444.5675555.678

然而,随着
ttk.Scale
数字和
分辨率的消失。如果将
tk.DoubleVar
用作刻度变量,则留下一个很长的十进制数。因此,根据这篇文章:

我必须为标签编写我自己的数字显示方案,当我已经使用一个widget类时,这看起来很疯狂。我还没有看到如何保持DoubleVar,因为我需要更改它的字符串表示形式

有没有更简单的方法来实现我想要的

更新:

代码如下:

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
mainframe = tk.Frame(root)

# Model

input = tk.DoubleVar(value=0.)

spin = tk.Spinbox(mainframe, textvariable=input, wrap=True, width=10)
slide = ttk.Scale(mainframe, variable=input, orient='horizontal', length=200)
spin['to'] = 1.0
spin['from'] = 0.0
spin['increment'] = 0.01
slide['to'] = 1.0
slide['from'] = 0.0
# slide['digits'] = 4
# slide['resolution'] = 0.01

# Layout

weights = {'spin': 1, 'slide': 100}

mainframe.grid_rowconfigure(0, weight=1)
mainframe.grid_columnconfigure(0, weight=weights['spin'])
mainframe.grid_columnconfigure(1, weight=weights['slide'])
spin.grid(row=0, column=0, sticky='news')
slide.grid(row=0, column=1, sticky='news')

root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
mainframe.grid(row=0, column=0)

root.mainloop()
当我拖动刻度时,十进制数字突然失去控制


这里有一个比我第一个(现已删除)答案更好的解决方案,我觉得这是一个更干净、更好的方法,可以以更面向对象的方式实现功能,因此不需要像我最初的答案那样进行黑客攻击

相反,它通过定义名为“
Limiter
”的
ttk.Scale
子类I来完成所需的工作,该子类支持名为
precision
的附加关键字参数

import tkinter as tk
import tkinter.ttk as ttk


root = tk.Tk()
mainframe = tk.Frame(root)


# Model

class Limiter(ttk.Scale):
    """ ttk.Scale sublass that limits the precision of values. """

    def __init__(self, *args, **kwargs):
        self.precision = kwargs.pop('precision')  # Remove non-std kwarg.
        self.chain = kwargs.pop('command', lambda *a: None)  # Save if present.
        super(Limiter, self).__init__(*args, command=self._value_changed, **kwargs)

    def _value_changed(self, newvalue):
        newvalue = round(float(newvalue), self.precision)
        self.winfo_toplevel().globalsetvar(self.cget('variable'), (newvalue))
        self.chain(newvalue)  # Call user specified function.


# Sample client callback.
def callback(newvalue):
    print('callback({!r})'.format(newvalue))

input_var = tk.DoubleVar(value=0.)
spin = tk.Spinbox(mainframe, textvariable=input_var, wrap=True, width=10)
slide = Limiter(mainframe, variable=input_var, orient='horizontal', length=200,
                command=callback, precision=4)

spin['to'] = 1.0
spin['from'] = 0.0
spin['increment'] = 0.01
slide['to'] = 1.0
slide['from'] = 0.0
# slide['digits'] = 4
# slide['resolution'] = 0.01

# Layout

weights = {'spin': 1, 'slide': 100}

mainframe.grid_rowconfigure(0, weight=1)
mainframe.grid_columnconfigure(0, weight=weights['spin'])
mainframe.grid_columnconfigure(1, weight=weights['slide'])
spin.grid(row=0, column=0, sticky='news')
slide.grid(row=0, column=1, sticky='news')

root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
mainframe.grid(row=0, column=0)

root.mainloop()