0

I'm trying to create a window with multiple frames in tkinter. But the window title remains the same for any active frame. The code is used for reference is here.

I tried to add

self.container.title(title)

in respective Pages classes. but the window get the title which was specified the latest.


'''
Solution from...
https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
'''

import tkinter as tk                # python 3
from tkinter import font  as tkfont # python 3
#import Tkinter as tk     # python 2
#import tkFont as tkfont  # python 2

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        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 = {}
        for F in (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

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

        self.controller.title("Start Page")

        label = tk.Label(self, text="This is the start page", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame("PageOne"))
        button2 = tk.Button(self, text="Go to Page Two",
                            command=lambda: controller.show_frame("PageTwo"))
        button1.pack()
        button2.pack()


class PageOne(tk.Frame):

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

        self.controller.title("Page One")

        label = tk.Label(self, text="This is page 1", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


class PageTwo(tk.Frame):

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

        self.controller.title("Page Two")

        label = tk.Label(self, text="This is page 2", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

Here I expected it to have title of the window to change whenever the frame changed but it prints the title as "Page Two" for entire time even I jump from one frame to another.

Abhijit
  • 13
  • 1
  • 4

1 Answers1

1

Yes, you can use the parameter page_name to change the title of the app when a frame changes

def show_frame(self, page_name):
    '''Show a frame for the given page name'''
    frame = self.frames[page_name]
    frame.tkraise()
    self.title(page_name)     # update the app title
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • Thanks for the help! But what might be the reason that `self.container.title('title')` didn't worked as expected? – Abhijit Jun 11 '19 at 15:40
  • Good question: in this case, `self` in `self.title(page_name)` refers to the `SampleApp` instance, which is a subclass of `tk.Tk`, and therefore the main window of the application; `container` in `self.container.title('title')` refers to one of the `tk.Frame` of the application. `Frame` does not have a `title` attribute. – Reblochon Masque Jun 11 '19 at 15:50
  • Ok, but you say that title is not an attribute of frame, but it was working partially. As I had the **PageTwo** class at the last and it had `self.container.title("Page Two")` so it was making that title forever. Now if we remove that class it will make the title **Page One** as PageOne class now becomes the last class. – Abhijit Jun 11 '19 at 16:06