Python 如何从 Tkinter 滑块(“比例”)中获取价值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14508727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Get Value Out from the Tkinter Slider ("Scale")?
提问by user2006082
So, here is the code I have, and as I run it, the value of the slider bar appears above the slider, I wonder is there a way to get that value out? Maybe let a=that value. ;)
所以,这是我的代码,当我运行它时,滑块的值出现在滑块上方,我想知道有没有办法把这个值取出来?也许让 a=那个值。;)
from Tkinter import *
control = Tk()
control.geometry("350x200+100+50")
scale = Scale(control,orient=HORIZONTAL,length=300,width=20,sliderlength=10,from_=0,to=1000,tickinterval=100)
scale.pack()
control.mainloop()
回答by Martijn Pieters
Call the .get()method on the slider to read the current value:
调用.get()滑块上的方法读取当前值:
print scale.get()
See the Tkinter Scale widgetdocumentation.
请参阅Tkinter Scale 小部件文档。
回答by mmgp
To get the value as it is modified, associate a function with the parameter command. This function will receive the current value, so you just work with it. Also note that in your code you have cline3 = Scale(...).pack(). cline3is always None in this case, since that is what pack()returns.
要获取修改后的值,请将函数与参数 关联command。此函数将接收当前值,因此您只需使用它即可。另请注意,在您的代码中,您有cline3 = Scale(...).pack(). cline3在这种情况下总是 None ,因为那是pack()返回的内容。
import Tkinter
def print_value(val):
print val
root = Tkinter.Tk()
scale = Tkinter.Scale(orient='horizontal', from_=0, to=128, command=print_value)
scale.pack()
root.mainloop()
回答by mmgp
Idk if you have finally solved or not given age: but ive done a similar app for robot control. Basic code after gui was declared and initialized, and scale programmed: scale(blahblahblah) Scale.grid(blahblah)
不知道你是否最终解决了或没有给出年龄:但我为机器人控制做了一个类似的应用程序。gui声明初始化后的基本代码,以及scale编程: scale(blahblahblah) Scale.grid(blahblah)
Def forward():
motor1.forward(scale.get())
motor2.backward(scale.get())
Bear in mind: speed value requires percentage of 1, so set the scale to: from 0 to 1 with intervals being either .01 or .1(i used .1 for 10ths of my speed variable)
请记住:速度值需要 1 的百分比,因此将比例设置为:从 0 到 1,间隔为 .01 或 .1(我使用 .1 表示速度变量的 10 分之一)

