简体   繁体   English

如何在Python中检索tkinter ttk Scale小部件的整数值?

[英]How to retrieve the integer value of tkinter ttk Scale widget in Python?

Could anyone advise how to retrieve and update a Label widget with the value from a Scale widget in Python? 谁能建议如何使用Python中“ Scale小部件的值检索和更新“ Label小部件? Currently it shows a very large real number. 目前,它显示出非常大的实数。 I have tried to type cast the value but this only works when I print to idle. 我试图键入强制转换值,但这仅在我打印到空闲时有效。 I tried slider.get() but the label is blank. 我尝试了slider.get()但标签为空。 Also tried int(slider.get()) which works when I print to idle. 还尝试了int(slider.get()) ,当我打印到空闲时它可以工作。

from tkinter import *
from tkinter import ttk

root = Tk()
root.title("Playing with Scales")

mainframe = ttk.Frame(root, padding="24 24 24 24")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))

slider = IntVar()

ttk.Scale(mainframe, from_=0, to_=100, length=300,  variable=slider).grid(column=1, row=4, columnspan=5)
ttk.Label(mainframe, textvariable=slider).grid(column=1, row=0, columnspan=5)

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)


root.mainloop()

Here you can see the very large real value: 在这里,您可以看到非常大的实际价值:

截图

I don't see a way to control the resolution of the Scale widget, but it's fairly easy to modify the string it produces. 我没有控制Scale小部件分辨率的方法,但修改它产生的字符串相当容易。 According to these ttk docs the Scale widget returns a float, but in my experiments it returns a string, which is slightly annoying. 根据这些ttk文档 ,Scale小部件返回一个浮点数,但是在我的实验中,它返回一个字符串,这有点令人讨厌。

Rather than setting the Label text directly from the slider variable attached to the Scale widget we can bind a command to the Scale widget that converts the string holding the Scale's current value back to a float, and then format that float with the desired number of digits; 可以直接将命令绑定到Scale小部件,而不是直接从Scale小部件上附加的slider变量设置Label文本,该命令将将Scale的当前值转换为浮点数,然后使用所需位数进行格式化; the code below displays 2 digits after the decimal point. 下面的代码在小数点后显示2位数字。

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("Playing with Scales")

mainframe = ttk.Frame(root, padding="24 24 24 24")
mainframe.grid(column=0, row=0, sticky=('N', 'W', 'E', 'S'))

slider = tk.StringVar()
slider.set('0.00')

ttk.Scale(mainframe, from_=0, to_=100, length=300, 
    command=lambda s:slider.set('%0.2f' % float(s))).grid(column=1, row=4, columnspan=5)

ttk.Label(mainframe, textvariable=slider).grid(column=1, row=0, columnspan=5)

for child in mainframe.winfo_children(): 
    child.grid_configure(padx=5, pady=5)

root.mainloop()

If you just want the Label to display the integer value then change the initialization of slider to 如果只希望Label显示整数值,则将slider的初始化更改为

slider.set('0')

and change the callback function to 并将回调函数更改为

lambda s:slider.set('%d' % float(s))

I've made a few other minor changes to your code. 我对您的代码做了其他一些小的更改。 Primarily, I replaced the "star" import with import tkinter as tk ; 首先,我用import tkinter as tk代替了“ star” import; the star import brings in over 130 names into your namespace, which creates a lot of unnecessary clutter, as I mentioned in this recent answer . 正如我在最近的回答中提到的,star导入将超过130个名称引入到您的命名空间中,这会造成很多不必要的混乱。

An other less complicated option and easy to use alternative is to use an instance of tkinter.Scale() class with which you can inject the resolution option to whatever value you want. 另一个不太复杂且易于使用的替代方法是使用tkinter.Scale()类的实例,通过该实例,您可以将resolution选项注入所需的任何值。 The default one is the one you are looking for because its value is 1 默认值是您要查找的值,因为它的值为1

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

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