Skip to main content
1 of 2
Bryan Oakley
  • 2.1k
  • 13
  • 16

I don't exactly know what you mean by "call" the object. Objects aren't typically something you call. I'm going to assume that in this context you meant "show".

The code you copied was for having completely different "pages" which could be shown by name in any order. However, you want several identical "periods" that you want to access sequentially, or perhaps by number (eg: "show period 1", "show period 2", etc.)

So, assuming that each period is identical except for it's number, the first step is to create a single Period class. It seems reasonable that each period know it's number so you might want to add that to the constructor:

class Period(tk.Frame):
    def __init__(self, parent, controller, number):
        tk.Frame.__init__(self, parent)
        self.number = number
        ...

Next, in your Game, instead of looping over a list of classes, you need a simpler loop that runs as many times as you want to have periods. For example, if you are creating a game with four periods, you might modify the loop in Game to look like this:

self.periods = []
for period_number in range(4):
    self.periods.append(Period(container, self, period_number))

With that, self.periods[0] refers to the first period, self.periods[1] is the second, and so on.

You then need to modify show_frame to take a number rather than a class or name of a class. It should also be renamed to something like show_period to make the intent more clear (I've left error checking out to keep it short):

def show_period(self, period_number):
    frame = self.frames[period_number]
    frame.tkraise()

With that, you now have the ability to show by number any period you want. Since you want to be able to step through them sequentially, I recommend creating a method named next_period which can show the next period and handle any logic about what to do after the last period.

The simplest implementation will be to have show_period remember what the current period is, and then in next_period you simply need to add one:

def show_period(self, period_number):
    ...
    self.current_period = period_number

The implementation of next_period is pretty easy:

def next_period(self):
    if self.current_period == len(self.periods):
        <special code to handle the last period...>
    self.show_period(self.current_period+1)

With that, you can add a button to your Period class that will automatically take it to the next period, without it having to know what the next period is:

class Period(tk.Frame):
    def __init__(self, parent, controller, number):
        ...
        self.next_button = tk.Button(self, text="Next", command=self.next)
        ...

    def next(self):
        self.controller.next_period()

Note: This is not necessarily the best way to structure this program, it is simply one way of doing it. The point was to illustrate how to create multiple instances of a single class, and step through them one at a time.

Bryan Oakley
  • 2.1k
  • 13
  • 16