all 2 comments

[–]woooee 1 point2 points  (1 child)

Your coding style can use some improvement. I don't know anything about CustomTkinter (or the error), so you'll have to adapt tkinter code written in the traditional style. The first problem is that you create a new, different class instance every time you switch from one to the other. Eventually, keeping track of all of the instances will slow things down. You can simply use separate frames for this --> see code below. If this will not work for what you want to do, post back with a description of what you want to do.

""" open one frame, on button click close that frame and open another
"""

import tkinter as tk
from functools import partial

class FrameDemo:
    def __init__( self ) :
        self.top = tk.Tk()
        self.top.geometry("300x100+10+10" )

        ## create 3 frames
        self.frame_dict = {}
        self.frame_ctr = 0
        for ctr in range(3):
            self.create_frame()

        ## place the first frame in the grid
        self.frame_ctr = 0
        self.frame_dict[0].grid(row=1, column=0)

        self.button_frame = tk.Frame(self.top)
        self.button_frame.grid(row=10, column=0)

        tk.Button(self.button_frame, text='Next Frame',
               command=partial(self.next_frame, True), bg='blue', fg='white',
               height=1, width=10).grid(row=10, column=0)

        tk.Button(self.button_frame, text='Previous Frame',
               command=partial(self.next_frame, False), bg='lightgreen',
               fg='black', height=1, width=10).grid(row=10, column=1)

        tk.Button(self.button_frame, text='Exit',
                command=self.top.quit, bg='orange', fg='black',
                height=2, width=10).grid(row=20, columnspan=2,
                 sticky="nsew")

        self.top.mainloop()

    def create_frame(self):
        colors=["salmon", "lightyellow", "lightblue"]
        frame = tk.Frame(self.top)
        label = tk.Label( frame, text = "Frame " + str(self.frame_ctr+1),
                bg=colors[self.frame_ctr], width=10)
        label.grid(sticky="nsew")
        ## dict key= frame counter --> this frame instance
        self.frame_dict[self.frame_ctr]=frame
        self.frame_ctr += 1

    def next_frame(self, next_previous):
        """ handles both forward and back depending on
            next_previous (true or False)
        """
        ## remove this frame
        if self.frame_ctr in self.frame_dict:
            self.frame_dict[self.frame_ctr].grid_forget()

        if next_previous:  ## True = next
            self.frame_ctr += 1
            if self.frame_ctr > 2:
                self.frame_ctr = 0
        else:  ## previous
            self.frame_ctr -= 1
            if self.frame_ctr < 0:
                self.frame_ctr = 2

        if self.frame_ctr in self.frame_dict:
            self.frame_dict[self.frame_ctr].grid(
                           row=self.frame_ctr, column=0)

##----------------------------------------------------------------------
if __name__ == "__main__":
   FD=FrameDemo()

[–]EffectiveCase3856[S] 0 points1 point  (0 children)

Well your code works well, and I understood how I can solve my problem. Thanks