import tkinter as tk
Why not from tkinter import * ? Because this is the wrong way to do it. Global imports are bad . Tkinter tutorials tend to do it wrong for some reason that I don't understand.
The reason for the as tk part is because you can make tk.Frame , not Tkinter.Frame , which makes the code a little easier to type and a little easier to read. This is completely optional.
class SampleApp(tk.Tk):
tk is the name of the imported tkinter module. Tk is the name of the class in this module that represents the root window. Each tkinter application must have one root window. By placing it in SampleApp , a subclass of this widget is created - a copy with additional functions.
No need to inherit from tk.Tk You can inherit from tk.Frame , any other tkinter widget, or even object . This is a personal preference. Choosing makes things easier, some things more difficult.
container = tk.Frame(self)
The above example creates an instance of the Frame widget that will be used as a container for other "pages". These "pages" will be located one above the other in this container.
frame = F(container, self)
F is a loop variable. The cycle repeats over the list of classes, so each time through the cycle F will represent the class. F(...) creates an instance of the class. For these classes ( StartPage , PageOne , PageTwo ) two parameters are required: a widget that will be the parent of this class, and an object that will be the server as a controller (a term borrowed from the model / view / controller).
The line of code creates an instance of the class (which itself is a subclass of the Frame widget) and temporarily assigns a frame to the local variable Frame .
By passing self as the second ("controller"), these new class instances will be able to call methods on the SampleApp class SampleApp .
self.frames[page_name] = frame
This saves the link to the frame just created in the dictionary. The key to the dictionary is the name of the page (or rather, the name of the class).
Here's how the show_frame method can determine a valid page widget only on behalf of the class.
frame.tkraise()
In almost all GUI tools - tkinter is included - there is the concept of "stacking order": the order in which things are stacked. Some tools may call this z-order. If two or more widgets are stacked on top of each other (which this code does by placing all pages on the same row and column), the widget that sits at the top of the stack is the widget that will usually be visible.
tkraise is a method of the Frame object that raises the frame at the top of the stacking order. In this line of code, Frame refers to one specific instance of one of the pages.
tk.Frame.__init__(self, parent)
Since each page is a subclass of the tk.Frame class, this calls the constructor of the parent class. This is necessary to initialize all the internal structures that make up the actual frame widget. Although Frame can take many options, this code decides to send only one thing - a link to another widget, which should act as the parent of this new widget.
self.controller = controller
The above code simply โremembersโ the value of the controller variable that was passed. In this case, the controller is the application. Keeping this, this class can call methods on the SampleApp object.
Note: the code in the question came from a tutorial that copied the code from this answer: https://stackoverflow.com/a/416829/ I am the author of this original code, but not the author of the textbook. This original answer has links to other questions related to this code.