猿问

按住tkinter按钮时尝试保持功能恒定运行

我目前在tkinter中有一个按钮,可以在释放按钮时运行功能。我需要按钮在按住按钮的整个过程中,以一定的速率不断增加一个数字。


global var

var=1

def start_add(event,var):

    global running

    running = True

    var=var+1

    print(var)

    return var


def stop_add(event):

    global running

    print("Released")

    running = False

button = Button(window, text ="Hold")

button.grid(row=5,column=0)

button.bind('<ButtonPress-1>',start_add)

button.bind('<ButtonRelease-1>',stop_add)

我不必在释放按钮时就需要运行任何功能,只要按住按钮就可以了(如果有帮助的话)。任何帮助深表感谢。


慕无忌1623718
浏览 206回答 2
2回答

幕布斯7119047

没有内置的功能可以做到这一点,但是制作自己的Button可以很容易。您也走在正确的轨道上,唯一缺少的是需要使用它after来进行循环和after_cancel停止循环:try:&nbsp; &nbsp; import tkinter as tkexcept ImportError:&nbsp; &nbsp; import Tkinter as tkclass PaulButton(tk.Button):&nbsp; &nbsp; """&nbsp; &nbsp; a new kind of Button that calls the command repeatedly while the Button is held&nbsp; &nbsp; :command: the function to run&nbsp; &nbsp; :timeout: the number of milliseconds between :command: calls&nbsp; &nbsp; &nbsp; if timeout is not supplied, this Button runs the function once on the DOWN click,&nbsp; &nbsp; &nbsp; unlike a normal Button, which runs on release&nbsp; &nbsp; """&nbsp; &nbsp; def __init__(self, master=None, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; self.command = kwargs.pop('command', None)&nbsp; &nbsp; &nbsp; &nbsp; self.timeout = kwargs.pop('timeout', None)&nbsp; &nbsp; &nbsp; &nbsp; tk.Button.__init__(self, master, **kwargs)&nbsp; &nbsp; &nbsp; &nbsp; self.bind('<ButtonPress-1>', self.start)&nbsp; &nbsp; &nbsp; &nbsp; self.bind('<ButtonRelease-1>', self.stop)&nbsp; &nbsp; &nbsp; &nbsp; self.timer = ''&nbsp; &nbsp; def start(self, event=None):&nbsp; &nbsp; &nbsp; &nbsp; if self.command is not None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.command()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.timeout is not None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.timer = self.after(self.timeout, self.start)&nbsp; &nbsp; def stop(self, event=None):&nbsp; &nbsp; &nbsp; &nbsp; self.after_cancel(self.timer)#demo code:var=0def func():&nbsp; &nbsp; global var&nbsp; &nbsp; var=var+1&nbsp; &nbsp; print(var)root = tk.Tk()btn = PaulButton(root, command=func, timeout=100, text="Click and hold to repeat!")btn.pack(fill=tk.X)btn = PaulButton(root, command=func, text="Click to run once!")btn.pack(fill=tk.X)btn = tk.Button(root, command=func, text="Normal Button.")btn.pack(fill=tk.X)root.mainloop()正如@ rioV8所提到的,该after()调用不是非常准确。如果将超时设置为100毫秒,则通常可以在两次调用之间等待100到103毫秒之间的任何时间。按住按钮的时间越长,这些错误就会越多。如果要精确计时按钮的按下时间,则需要使用其他方法。

红糖糍粑

我认为@Novel的答案应该可以,但是这与您尝试的内容大致相同,不需要全新的课程:from tkinter import *INTERVAL=5 #miliseconds between runsvar=1def run():&nbsp; &nbsp; global running, var&nbsp; &nbsp; if running:&nbsp; &nbsp; &nbsp; &nbsp; var+=1&nbsp; &nbsp; &nbsp; &nbsp; print(var)&nbsp; &nbsp; &nbsp; &nbsp; window.after(INTERVAL, run)def start_add(event):&nbsp; &nbsp; global running&nbsp; &nbsp; running = True&nbsp; &nbsp; run()def stop_add(event):&nbsp; &nbsp; global running, var&nbsp; &nbsp; print("Released")&nbsp; &nbsp; running = Falsewindow=Tk()button = Button(window, text ="Hold")button.grid(row=5,column=0)button.bind('<ButtonPress-1>',start_add)button.bind('<ButtonRelease-1>',stop_add)mainloop()
随时随地看视频慕课网APP

相关分类

Python
我要回答