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:

import tkinter as tki

class App(object):

    def __init__(self):
        self.root = tki.Tk()        
        self.root.wm_title("Play With python")
        for r in range(8):
            self.root.rowconfigure(r, weight=1)
        for c in range(2):
            self.root.columnconfigure(c, weight=1)
    # Create Frames
        Frame1 = tki.Frame(self.root, borderwidth=1, bg = 'blue')
        Frame1.grid(row = 0, column = 0, rowspan = 2, columnspan = 2, sticky = 'w, e, n, s')

        Frame2 = tki.Frame(self.root, borderwidth=1, bg = 'blue')
        Frame2.grid(row = 2, column = 0, rowspan = 2, columnspan = 2, sticky = 'w, e, n, s')

        Frame3 = tki.Frame(self.root, borderwidth=1, bg = 'blue')
        Frame3.grid(row = 4, column = 0, rowspan = 2, columnspan = 2, sticky = 'w, e, n, s')

        Frame4 = tki.Frame(self.root, borderwidth=1, bg = 'blue')
        Frame4.grid(row = 6, column = 0, rowspan = 2, columnspan = 2, sticky = 'w, e, n, s')

app = App()
app.root.mainloop()

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:

import tkinter as tki

class App(object):

    def __init__(self):
        self.root = tki.Tk()        
        self.root.wm_title("Play With python")
        for r in range(8):
            self.root.rowconfigure(r, weight=1)
        for c in range(2):
            self.root.columnconfigure(c, weight=1)
    # Create Frames
        j = 0
        for i in range(1,5):
            Framei = tki.Frame(self.root, borderwidth=1, bg = 'blue')
            Framei.grid(row = j, column = 0, rowspan = 2, columnspan = 2, sticky = 'w, e, n, s') 
            j = j + 2
app = App()
app.root.mainloop()

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:

import tkinter as tki

class App(object):

    def __init__(self):
        self.root = tki.Tk()
        self.root.wm_title("Play With python")
        for r in range(8):
            self.root.rowconfigure(r, weight=1)
        for c in range(2):
            self.root.columnconfigure(c, weight=1)

        # Create a list of the Frames in the order they were created
        frames = []
        j = 0
        for i in range(1,5):
            Frame = tki.Frame(self.root, borderwidth=1, bg = 'blue')
            Frame.grid(row = j, column = 0, rowspan = 2, columnspan = 2, sticky = 'w, e, n, s')

            # Add the Frame to the list
            frames.append(Frame)

            # Also, just as an FYI, j = j + 2 can be better written like this
            j += 2

        # To demonstrate
        print(frames)

        # This is the first Frame created
        print(frames[0])

app = App()
app.root.mainloop()

To access the Frames, just index the list.

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