GUI 按钮按住 - tkinter [英] GUI Button hold down - tkinter
问题描述
我正在尝试在 python 中做一个 GUI 来控制我的机器人汽车.我的问题是我如何执行确定按住按钮的功能.我想在按住按钮时移动汽车并在释放按钮时停止汽车.
I'm trying to do a GUI in python to control my robotic car. My question is how I do a function that determine a hold down button. I want to move the car when the button is pressed and held down and stop the car when the button is released.
from Tkinter import *
hold_down = False
root = Tk()
def button_hold(event):
hold_down=true
while hold_down== True:
print('test statement')
hold_down = root.bind('<ButtonRelease-1>',stop_motor)
def stop_motor(event):
hold_down= False
print('button released')
button = Button(root, text ="forward")
button.pack(side=LEFT)
root.bind('<Button-1>',button_forward)
root.mainloop()
我正在尝试模拟我在此答案
我尝试在带有布尔值的 while
循环中执行此操作.当用户按下按钮时,布尔值变为 True
并且代码进入 while 循环.当用户释放按钮时,布尔值更改为 False
并且代码从循环中退出,但在此代码中,无论我是否释放按钮,布尔值始终为真.
I try to do it in a while
loop with a boolean. When the user presses the button the boolean changes to True
and code enters the while loop. When user releases the button the boolean changes to False
and code exits from loop but in this code the boolean stay always true no matter if I released the button or not.
我想调用一个函数直到条件发生.要调用的函数是hold_down(),要检查的条件是按钮被释放.
I want a function to be called until a condition occurs.The function to be called is hold_down() and the condition to check is the button is released.
更新:我找到了让它工作的方法.
Update: I found a way to make it work.
推荐答案
按下按钮时设置标志,释放按钮时取消设置标志.不需要循环,因为您已经在运行循环 (mainloop
)
Set a flag when the button is pressed, unset the flag when the button is released. There's no need for a loop since you're already running a loop (mainloop
)
from Tkinter import *
running = False
root = Tk()
def start_motor(event):
global running
running = True
print("starting motor...")
def stop_motor(event):
global running
print("stopping motor...")
running = False
button = Button(root, text ="forward")
button.pack(side=LEFT)
button.bind('<ButtonPress-1>',start_motor)
button.bind('<ButtonRelease-1>',stop_motor)
root.mainloop()
假设您确实想在按键被按下时做某事,您可以使用 after
设置一个动画循环.例如,要在按下按钮时每秒调用一次打印语句,您可以添加一个执行打印语句的函数,然后安排自己在一秒钟后调用.停止按钮只需要取消任何挂起的作业.
Assuming that you actually want to do something while the key is pressed, you can set up an animation loop using after
. For example, to call a print statement once a second while the button is pressed you can add a function that does the print statement and then arranges for itself to be called one second later. The stop button merely needs to cancel any pending job.
这是一个例子.与原始代码的主要区别在于添加了 move
函数.我还添加了第二个按钮来展示如何使用相同的功能前进或后退.
Here's an example. The main difference to the original code is the addition of a move
function. I also added a second button to show how the same function can be used to go forward or backward.
from Tkinter import *
running = False
root = Tk()
jobid = None
def start_motor(direction):
print("starting motor...(%s)" % direction)
move(direction)
def stop_motor():
global jobid
root.after_cancel(jobid)
print("stopping motor...")
def move(direction):
global jobid
print("Moving (%s)" % direction)
jobid = root.after(1000, move, direction)
for direction in ("forward", "backward"):
button = Button(root, text=direction)
button.pack(side=LEFT)
button.bind('<ButtonPress-1>', lambda event, direction=direction: start_motor(direction))
button.bind('<ButtonRelease-1>', lambda event: stop_motor())
root.mainloop()
这篇关于GUI 按钮按住 - tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!