Change BTN color BG with cycle [Python]

Solution for Change BTN color BG with cycle [Python]
is Given Below:

im new at this and I am trying to get to the next color every time i click on the BTN.
I did it with the function “random.choice” and worked perfectly but now I want to get the next color instead of a random one.

import tkinter as tk
from itertools import cycle

mywindow = tk.Tk()

def changeBG():
    colors = ["red", "blue", "green", "yellow"]
    ciclo = cycle(colors)
    siguiente = next(ciclo)
    main_btn.config(bg=siguiente)

main_btn = tk.Button(mywindow, text="Change BTN Color", command=changeBG)
main_btn.place(x=50, y=80)
mywindow.mainloop()
import tkinter as tk
import random

mywindow = tk.Tk()
def changeBG():
    colors = ["red", "blue", "green" ]
    random_colors = random.choice(colors)
    main_btn.config(bg = random_colors)

main_btn = tk.Button(mywindow, text="Change BTN Color", command=changeBG)
main_btn.place(x=40, y=50)
mywindow.mainloop()

You need to create ciclo a single time. Otherwise you end up re-initializing it each time.

def changeBG():
    siguiente = next(ciclo)
    main_btn.config(bg=siguiente)

colors = ["red", "blue", "green", "yellow"]
ciclo = cycle(colors)

the currentColor variable cycles through numbers between 0 and 3 and changes the color of the BG.


mywindow = tk.Tk()

colors = ['red', 'blue', 'green', 'yellow']
currentColor = -1
def changeBG():
    global currentColor
    currentColor += 1
    if currentColor > 3:
        currentColor =0
    main_btn.config(bg=colors[currentColor])

main_btn = tk.Button(mywindow,text="Change BTN Color",command=lambda:changeBG())
main_btn.place(x=50,y=80)

mywindow.mainloop()