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


Messages In This Thread
Have tkinter button toggle on and off a continuously running function - by AnotherSam - Oct-01-2021, 02:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 802 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 739 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Using Tkinter inside function not working Ensaimadeta 5 4,858 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,446 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,369 May-25-2023, 07:37 PM
Last Post: deanhystad
  help needed running a simple function in pyqt5 diodes 27 8,162 Jan-24-2023, 12:19 PM
Last Post: GetOnData
  Tkinter won't run my simple function AthertonH 6 3,739 May-03-2022, 02:33 PM
Last Post: deanhystad
  Can't get tkinter button to change color based on changes in data dford 4 3,361 Feb-13-2022, 01:57 PM
Last Post: dford
  tkinter toggle buttons not working Nu2Python 26 6,767 Jan-23-2022, 06:49 PM
Last Post: Nu2Python
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,733 Nov-17-2021, 03:21 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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