Python Forum
[Tkinter] Have tkinter button toggle on and off a continuously running function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Have tkinter button toggle on and off a continuously running function
#1
Hello all,

I am trying to write a function for a tkinter button that when toggled begins running a continuously looping function until the same button is toggled again. The code below is where i started. When the button is pressed "looping" gets continuously printed until i close the window. However, I'd rather the function stop being called after the same button is pressed again. Thanks in advance!

from tkinter import *

def close():
    win.destroy()
    
def loopFunction():
        print("looping")
        win.after(1,loopFunction)


win = Tk()

InitButton = Button(win,text='Loop Function',command= loopFunction , bg = "bisque2") #,bg='grey')
InitButton.grid(row=0, column=0, pady = 5)

win.protocol("WM_DELETE_WINDOW", close)
win.mainloop()
Reply
#2
Tkinter buttons don't toggle. How about a checkbox?
Reply
#3
Something like this?
import tkinter as tk
from itertools import cycle


class MainFrame(tk.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, *kwargs)
        self._timer = False
        self._button1_state = cycle(("disabled", "normal"))
        self._create()
        self._layout()
        self._binds()

    def _create(self):
        self.button1 = tk.Button(self, text="Button1")

    def _layout(self):
        self.button1.pack(padx=5, pady=5)

    def _binds(self):
        self.button1.bind("<ButtonRelease-1>", self._on_button1)

    def _on_button1(self, event):
        if not self._timer:
            self.toggle_button1()
        else:
            self.after_cancel(self._timer)
            self.button1.config(state="normal")
            self._timer = None

    def toggle_button1(self):
        self.button1.config(state=next(self._button1_state))
        self._timer = self.after(100, self.toggle_button1)


def main():
    app = tk.Tk()
    mainframe = MainFrame(app)
    mainframe.pack()
    app.mainloop()


if __name__ == "__main__":
    main()
Reply
#4
(Oct-01-2021, 03:38 AM)deanhystad Wrote: Tkinter buttons don't toggle. How about a checkbox?
Yes, that's the main issue here. I usually pass a variable to keep track of the button state, but not so in this case. After seeing the solution regarding the button, and how much code it takes, I am very interested in how you would do this with a check box.
Reply
#5
Do as you were doing, but only call loopFunction if the checkbox is ckecked
Reply
#6
I miss read the requirements, I thought you wanted a button toggle on and off a continuously Doh
Here is some shorter code hopefully more in line with what you asked for.
Note: if the loop_function does anything that takes too long it will block the GUI event loop see the following
[Tkinter] How to deal with code that blocks the mainloop, freezing the gui
import tkinter as tk


def loop_function():
    print("looping")


class MainFrame(tk.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, *kwargs)
        self._looping = False
        button1 = tk.Button(self, text="Button1", command=self._on_button1)
        button1.pack(padx=5, pady=5)
        self._timer()

    def _on_button1(self):
        self._looping = not self._looping

    def _timer(self):
        if self._looping:
            loop_function()
        self.after(100, self._timer)


def main():
    app = tk.Tk()
    mainframe = MainFrame(app)
    mainframe.pack()
    app.mainloop()


if __name__ == "__main__":
    main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 250 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 434 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  tkinter showing image in button rwahdan 3 1,350 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 1,510 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter get function finndude 2 923 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 1,750 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  tkinter python button position problem Nick_tkinter 3 1,244 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  TKinter restarting the mainloop when button pressed zazas321 7 4,144 Jan-26-2021, 06:38 AM
Last Post: zazas321
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 1,336 Dec-28-2020, 10:00 PM
Last Post: nanok66
  Using Tkinter inside function not working Ensaimadeta 3 1,234 Dec-23-2020, 02:57 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020