I'm trying to make a button that can pause and resume the loop.
In code:
for index in range(10):
print index
// Runs until here, and pause
// Button pressed
print index + 10
// Runs until here, and pause
// Button pressed
In terminal:
0
// Button pressed
10
// Button pressed
1
// Button pressed
11
...
9
// Button pressed
19
// Button pressed
Is there a way that I can do pause and resume the loop with a button?
You can use generators for this by calling next()
on each button click.
A little example of how:
import tkinter as tk
def plusten(x):
i = 0
while i<x:
yield i
yield i+10
i += 1
def next_item():
if gen:
try:
lbl["text"] = next(gen) #calls the next item of generator
except StopIteration:
lbl["text"] = "End of iteration" #if generator is exhausted, write an error
else:
lbl["text"] = "start loop by entering a number and pressing start loop button"
def start_gen():
global gen
try:
gen = plusten(int(ent.get()))
lbl["text"] = "loop started with value: " + ent.get()
except ValueError:
lbl["text"] = "Enter a valid value"
gen = None
root = tk.Tk()
ent = tk.Entry()
ent.pack()
tk.Button(root, text="start loop", command=start_gen).pack()
tk.Button(root, text="next item", command=next_item).pack()
lbl = tk.Label(root, text="")
lbl.pack()
root.mainloop()
''' Import the following in the code from tkinter import * import time
The following programme creates a tkinter window with two buttons (Pause and Quit),simultaneously printing a sequence of numbers from 1 to numb(numb=20, for illustration here) with a controllable time delay between prints (2 secs for illustration here). Pressing the Pause button stops the printing, changing the button text to Resume. After a while when the Resume button is pressed, the printing continues. To quit printing, the quit button is pressed. In the illustration, printing a number is symbolic. It could as well be display of images with time time delays. When it is desired to view an image in more detail, the display can be paused when that image is displayed and resumed when desired to view remaining images. ''' from tkinter import * import time
global move,stop
move = 1 #this a variable which can take a value 1 or 0
stop='Initial-Value' #This is a variable to control the for and while loops.
#Assignment of string as variable is optional. It could be
#integer as well
def switch(): #controls back and forth switching between Pause and Resume
global move
move = not move
def come_out(): # controls the process to be running or quit
global stop
stop='stop'
numb=20
root = Tk()
Pause_Button=Button(root,bg='wheat',command=switch)
Pause_Button.place(relwidth=0.25,relx=0.2,rely=0.2)
Quit_Button=Button(root,bg='lightblue',text='Quit',command=come_out)
Quit_Button.place(relwidth=0.25,relx=0.5,rely=0.2)
time.sleep(2)
for s in range(numb):
if stop=='stop':
break
stop='continue_while_loop'
while s<numb and stop=='continue_while_loop':
stop='enter_for_loop_again'
Pause_Button.update()
if move==1:
Pause_Button["text"]='Pause'
print('s2=',s,'\n')
time.sleep(2)
else:
stop='continue_while_loop'
Pause_Button["text"]='Resume'
Quit_Button.update()
root.mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.