Python Forum

Full Version: Creating a variables inside FOR loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I would like to know what is really the most efficient and best way to create multiple variables inside for loop. At the moment, I am creating 20 variables one by one and that is not a massive problem for me. See the code below:

def picking_gui():
    number=count_unique_devices(myConnection)
    print("number=",number)
    global device1,device2,device3,device4,device5,device6,device7,device8,device9,device10,device11,device12,device13,device14,device15,device16,device17,device18,device19,device20
    #global canvas_tk
    device1 = canvas_tk.create_rectangle(20,400,90,470,fill='red')
    canvas_tk.create_text(55,435,text="device1")
    device2 = canvas_tk.create_rectangle(110,400,180,470,fill='red')
    canvas_tk.create_text(145,435,text="device2")
    device3 = canvas_tk.create_rectangle(210,400,280,470,fill='red')
    canvas_tk.create_text(245,435,text="device3")
    device4 = canvas_tk.create_rectangle(310,400,380,470,fill='red')
    canvas_tk.create_text(345,435,text="device4")
    device5 = canvas_tk.create_rectangle(410,400,480,470,fill='red')
    canvas_tk.create_text(445,435,text="device5")
    device6 = canvas_tk.create_rectangle(510,400,580,470,fill='red')
    canvas_tk.create_text(545,435,text="device6")
    device7 = canvas_tk.create_rectangle(610,400,680,470,fill='red')
    canvas_tk.create_text(645,435,text="device7")
    device8 = canvas_tk.create_rectangle(710,400,780,470,fill='red')
    canvas_tk.create_text(745,435,text="device8")
    device9 = canvas_tk.create_rectangle(810,400,880,470,fill='red')
    canvas_tk.create_text(845,435,text="device9")
    device10 = canvas_tk.create_rectangle(910,400,980,470,fill='red')
    canvas_tk.create_text(945,435,text="device10")
    device11 = canvas_tk.create_rectangle(20,500,90,570,fill='red')
    canvas_tk.create_text(55,535,text="device11")
    device12 = canvas_tk.create_rectangle(110,500,180,570,fill='red')
    canvas_tk.create_text(145,535,text="device12")
    device13 = canvas_tk.create_rectangle(210,500,280,570,fill='red')
    canvas_tk.create_text(245,535,text="device13")
    device14 = canvas_tk.create_rectangle(310,500,380,570,fill='red')
    canvas_tk.create_text(345,535,text="device14")
    device15 = canvas_tk.create_rectangle(410,500,480,570,fill='red')
    canvas_tk.create_text(445,535,text="device15")
    device16 = canvas_tk.create_rectangle(510,500,580,570,fill='red')
    canvas_tk.create_text(545,535,text="device16")
    device17 = canvas_tk.create_rectangle(610,500,680,570,fill='red')
    canvas_tk.create_text(645,535,text="device17")
    device18 = canvas_tk.create_rectangle(710,500,780,570,fill='red')
    canvas_tk.create_text(745,535,text="device18")
    device19 = canvas_tk.create_rectangle(810,500,880,570,fill='red')
    canvas_tk.create_text(845,535,text="device19")
    device20 = canvas_tk.create_rectangle(910,500,980,570,fill='red')
    canvas_tk.create_text(945,535,text="device20")
I am creating 20 rectangles and assigning a different name to each one. The reason why I am assigning a name to each rectangle and declaring it as global, is because I need to be able to access and modify these rectangles in other functions such as:
def update_rectangle_color(device_name,last_device):
    canvas_tk.itemconfig(eval(last_device),fill="red")
    print("updating rectangle colour=",device_name)
    canvas_tk.itemconfig(eval(device_name),fill="green")
    print("item updated")
    last_device = device_name
    return last_device
in the function above, I pass the device name, for example "device4" and I will change the colour of that rectangle.

My code works fine and as expected, the problem is that everytime the program starts, I calculate how many unique devices I have ( it might be 5, 10, 15 or whatever) and I want to generate the number of rectangles based on the number that has been returned from the function:
number=count_unique_devices(myConnection)
    print("number=",number)
I am thinking whether I could use a for loop or simmilar to dynamically create and initialze only the required amount of rectangle everytime?
Don't create 20 different variables. Create one variable that can hold 20 objects (a list or a dictionary)
also, in addition to @bowlofred's excellent advise - don't use globals. Make GUI using OOP approach (i.e. with class)
Thanks everyone, I have managed to create variables using for loop with dictionaries as bowlofred mentioned:

def picking_gui():
    number=count_unique_devices(myConnection)
    print("number=",number)
    global devices
    devices = {}
    for x in range(1,number):
        devices["device{0}".format(x)] = canvas_tk.create_rectangle(20+(x*90),400,90+(x*90),470,fill='red')
        name_text = "device"+str(x)
        canvas_tk.create_text(55+(x*90),435,text=name_text)
        if(x>10):
            devices["device{10}".format(x)] = canvas_tk.create_rectangle(20+(x*90),500,90+(x*90),570,fill='red')
I now will have a look at how to create my GUI using OOP aproach as I have never used it before. It seems more complex than the usual program flow I am used to
i suggest you to use the zip function as well. It will help declare multiple tuples in a single for loop sentence.
(Sep-11-2020, 06:30 AM)bowlofred Wrote: [ -> ]Don't create 20 different variables. Create one variable that can hold 20 objects (a list or a dictionary)


Yes agreed with you. We have to create variable with different objects and this way, our code will be managed very easily.