在Tkinter中,我怎样才能不断得到滑块值

2024-05-14 03:46:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚开始为我正在进行的一个机器人项目制作一个GUI界面,我已经搁浅了。我想让我的Tkinter小部件中的滑块打印当前位置/值,只要它被调整。现在,唯一能不断获得输入的方法就是手动按下一个按钮,为我调出这些信息。我以为获取这些数据的方法是在运行主循环之后运行Throttle.get(),但这只会在我关闭小部件之前执行。我对Tk还比较陌生,但这是我目前为止的脚本。在

from Tkinter import *
master = Tk()

def getThrottle(): # << I don't want to use a button, but I am in this case.
    print Throttle.get()

Throttle = Scale(master, from_=0, to=100, orient=HORIZONTAL)
Throttle.set(0)
Throttle.pack()

getB = Button(master, text ="Hello", command = getThrottle)  
getB.pack()

mainloop()

Tags: to项目方法frommasterget部件tkinter
1条回答
网友
1楼 · 发布于 2024-05-14 03:46:16

只需设置天平的命令选项即可:

from Tkinter import *
master = Tk()

def getThrottle(event):
    print Throttle.get()

Throttle = Scale(master, from_=0, to=100, orient=HORIZONTAL, command=getThrottle)
Throttle.set(0)
Throttle.pack()

mainloop()

现在,当你移动电子秤时,数据会实时打印在终端上(无需按下按钮)。在

相关问题 更多 >