-1

I am trying to create an application where within the main frame, there are two other frames (side to side).

The left frame consist of buttons that are always visible. Those buttons will change the contents of the right frame.

I have done this successfully by assigning each button to a definition which recreates the buttons in the left frame, clears the content on the right frame and rewrite what goes into the right frame.

This works, but I would love to learn more on different/more efficient ways of doing this.

I have been searching for the last couple of days on how I can include classes and this is what I have found.

class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        frame = StartPage(container, self)

        self.frames[StartPage] = frame

        frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

This class helps create a controller where you can navigate through frames.

The problem i'm having is creating a frame that is always visible since, def show_frame changes the contents of the entire window.

I had an idea where I can add this structure into a class called 'RightFrame' that isn't the main window, but I have no idea how I would make the frames and buttons communicate with each other.

I am a complete beginner at coding so any pointers would be appreciated.

B Food
  • 75
  • 1
  • 8

1 Answers1

0

tkraise() just brings the widget it's called on to the front, which means it will only change that part of the window where the widget it's called on is in.

All the frames that can be changed between are in container, so raising any of those frames will only affect the part of the window where container is. You can therefore put in another frame next to container, which will not be affected by any of the frame changing inside container:

left_frame = tk.Frame(self)
left_frame.pack(side="left", fill="both", expand = True)
container = tk.Frame(self)
container.pack(side="left", fill="both", expand = True)

label = tk.Label(left_frame, text="This is the left frame", font=self.title_font)
label.pack(side="top", fill="x", pady=10)

or

left_frame = LeftFrame(self)
left_frame.pack(side="left", fill="both", expand = True)
container = tk.Frame(self)
container.pack(side="left", fill="both", expand = True)

class LeftFrame(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This is the left frame", font=parent.title_font)
        label.pack(side="top", fill="x", pady=10)
fhdrsdg
  • 9,027
  • 2
  • 32
  • 52