Python Forum
Creating a variables inside FOR loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a variables inside FOR loop
#1
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?
Reply
#2
Don't create 20 different variables. Create one variable that can hold 20 objects (a list or a dictionary)
Reply
#3
also, in addition to @bowlofred's excellent advise - don't use globals. Make GUI using OOP approach (i.e. with class)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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
Reply
#5
i suggest you to use the zip function as well. It will help declare multiple tuples in a single for loop sentence.
Reply
#6
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 443 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,727 Nov-07-2023, 09:49 AM
Last Post: buran
  Help adding a loop inside a loop Extra 31 4,553 Oct-23-2022, 12:16 AM
Last Post: Extra
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,496 Jul-27-2022, 08:50 PM
Last Post: rob101
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,494 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  how to use 3 variables python loop evilcode1 2 1,675 Nov-12-2021, 11:43 AM
Last Post: jamesaarr
  creating a loop yk303 2 1,865 Feb-08-2021, 08:41 PM
Last Post: nilamo
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,718 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop JulyFire 2 2,533 Jan-10-2021, 01:50 AM
Last Post: JulyFire
  Two variables in loop Ferdis 1 1,543 Jul-24-2020, 10:18 AM
Last Post: buran

Forum Jump:

User Panel Messages

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