Python Forum
[Tkinter] How to terminate a loop in python with button in GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to terminate a loop in python with button in GUI
#5
I am using tkinter. Here is my code. There are two buttons on the GUI. STRAT and STOP. If I press START then a counter up to 30 should be printed on the shell but if I press STOP anytime before the counter reach 30 then the loop should terminate and back to the man loop of GUI. Where should I add thread ?

import time

from tkinter import * 
root = Tk()
root.title("Loop Terminate")

time.sleep(0.5)

# Function button_stop 
def button_stop():
# If the STOP button is pressed then terminate the loop
  i = 1

# Function button_start 
def button_start():
  j = 1
  while j <= int(30):
    print("Loop Index = " + str(j))
    time.sleep(0.5)
    j = j+1

# Button START
button_start =  Button(root, text = "START", padx=53, pady=20, command=button_start)
button_start.grid(columnspan=1, row=1,column=0)

# Button STOP
button_stop =  Button(root, text = "STOP", padx=44, pady=20, command=button_stop)
button_stop.grid(row=2,column=0)
Reply


Messages In This Thread
RE: How to terminate a loop in python with button in GUI - by Joni_Engr - Sep-09-2021, 11:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Loop triggered by button help Purple0 1 2,408 May-17-2020, 02:57 AM
Last Post: deanhystad
  [Tkinter] Binding Entry box to <Button-3> created in for loop iconit 5 5,140 Apr-22-2020, 05:47 AM
Last Post: iconit
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,149 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] loop function when called from tkinter button click WantedStarling 5 9,173 Jul-13-2018, 06:12 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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