Skip to content
Advertisement

How to create multiple frames in python through For Loop?

I am working on GUI programming of python using Tkinter.
I am creating 4 frames(Frame1, Frame2, Frame3 and Frame4) in my Tkinter Root Window by using the below code:

JavaScript

The same thing I want to do using a for loop to make my code readable as in my real code the frames are around 12.

I am trying the below code for the same output:

JavaScript

Problem:

As I can see the problem this code is creating 4 frames but with same name reference as Framei(and not as Frame1, Frame2, Frame3 and Frame4). But I want 4 separate frames with different names as (Frame1, Frame2, Frame3 and Frame4).

I know my problem is quite related to variable creation at Run Time, but as I am applying it first time So facing Problem.

Python Version I am using is 3.0.1.
Please Help…

Advertisement

Answer

You should place the Frame instances in a list like so:

JavaScript

To access the Frames, just index the list.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement