Skip to main content
 首页 » 编程设计

python之Tkinter:使用 tk.Scale 和 tk.DoubleVar 控制 ttk.Scale 的增量

2024年12月31日10telwanggs

我曾经使用 tk.Scale 的 digits 属性来确保 Label 或 Spinbox 中的数字> 在 slider 移动时显示固定的小数位数。比如 3.456, 4444.567, 5555555.678, ...

但是,随着 ttk.Scale digits 和 resolution 消失了。如果将 tk.DoubleVar 用作比例变量,我将得到一个长十进制数。所以根据这篇文章:

How to make ttk.Scale behave more like tk.Scale?

我必须为标签编写我自己的数字显示方案,当我已经使用小部件类时,这看起来很疯狂。我还没有看到如何为此保留 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() 

当我拖动比例尺时,小数突然变得不受控制了。

请您参考如下方法:

这是一个比我的第一个(现已删除)答案中更好的解决方案,我认为这是一种以更面向对象的方式实现功能的更清晰、更好的方法——因此不需要像我原来那样的 hack一个做了。

相反,它通过定义一个 ttk.Scale 子类 来完成所需的工作,我将其命名为“Limiter”,它支持名为 精度。

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()