如何在Python中检索tkinter ttk Scale小部件的整数值? [英] How to retrieve the integer value of tkinter ttk Scale widget in Python?

查看:359
本文介绍了如何在Python中检索tkinter ttk Scale小部件的整数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以建议如何使用Python中Scale小部件的值检索和更新Label小部件吗?目前,它显示出非常大的实数.我试图键入强制转换值,但这仅在我打印到空闲时有效.我尝试了slider.get(),但标签为空.还尝试了int(slider.get()),当我将其打印到空闲状态时可以使用.

Could anyone advise how to retrieve and update a Label widget with the value from a Scale widget in Python? 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. Also tried int(slider.get()) which works when I print to idle.

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:

推荐答案

我没有控制Scale小部件分辨率的方法,但是修改它产生的字符串相当容易.根据这些ttk文档 Scale小部件返回一个浮点数,但是在我的实验中,它返回一个字符串,这有点烦人.

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. According to these ttk docs the Scale widget returns a float, but in my experiments it returns a string, which is slightly annoying.

我们可以直接将命令绑定到Scale小部件,而不是直接从Scale小部件附带的slider变量中设置Label文本,该命令将保存Scale当前值的字符串转换回float,然后使用所需的位数;下面的代码在小数点后显示两位数字.

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; the code below displays 2 digits after the decimal point.

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

如果只希望Label显示整数值,则将slider的初始化更改为

If you just want the Label to display the integer value then change the initialization of slider to

slider.set('0')

并将回调函数更改为

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


我对您的代码进行了其他一些小的更改.首先,我将导入替换为import tkinter as tk;正如我在最近的答案.


I've made a few other minor changes to your code. Primarily, I replaced the "star" import with import tkinter as tk; 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.

这篇关于如何在Python中检索tkinter ttk Scale小部件的整数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆