'Using exec to set button image in Python - is there a better way?

I'm writing a Python code for tkinter to set various images for four different buttons. I send to the method a list of four integers, 0-3, and it sets the image of the four buttons to image btn_img_A0, A1, A2, A3 (etc for the others) based on this list. I could write 16 if/elif statements to cover all possibilities, but I did this:

def set_btn_status():  #changes the buttons to reflect button_status
    global button_status
    image_for_A = 'btn_img_A' + str(button_status[0])
    exec ('button_A.configure(image = '+image_for_A+')')
    image_for_B = 'btn_img_B' + str(button_status[1])
    exec ('button_B.configure(image = '+image_for_B+')')
    image_for_C = 'btn_img_C' + str(button_status[2])
    exec ('button_C.configure(image = '+image_for_C+')')
    image_for_D = 'btn_img_D' + str(button_status[3])
    exec ('button_D.configure(image = '+image_for_D+')')

I'm told that using exec is risky, but this cuts the code to 8 lines instead of 16 and works fine - is this reasonable, or is there a better way??



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source