![]() I’ll be keeping the commands to implement the classes in a _name_="main", but, you can use it directly as well. If you’ve noticed, these classes are added to the main class, using the ames variable. Self, text = "Return to menu", command = lambda : controller. Label (self, text = "Completion Screen, we did it!" ) ![]() Frame ) : def _init_ (self, parent, controller ) : Label (self, text = "This is the Side Page" )Ĭommand = lambda : controller. ![]() pack (padx = 10, pady = 10 ) # We use the switch_window_button in order to call the show_frame() method as a lambda functionĬommand = lambda : controller. Now, we create different classes that act as Frames that are switched using the show_frame() method. frames # raises the current frame to the topįrame. Now that we’ve created the _init_ and have also specified the Frames that we’re going to use, we can create a method that switches the frame that we are viewing def show_frame (self, cont ) :įrame = self. grid (row = 0, column = 0, sticky = "nsew" ) # Using a method to switch frames for F in (MainPage, SidePage, CompletionScreen ) :įrame = F (container, self ) # the windows class acts as the root window for the frames.įrame. frames = # we'll create the frames themselves later but let's add the components to the dictionary. grid_columnconfigure ( 0, weight = 1 ) # We will now create a dictionary of frames grid_rowconfigure ( 0, weight = 1 )Ĭontainer. pack (side = "top", fill = "both", expand = True ) # configuring the location of the container using gridĬontainer. Frame (self, height = 400, width = 600 ) # specifying the region where the frame is packed in rootĬontainer. wm_title ( "Test Application" ) # creating a frame and assigning it to containerĬontainer = tk. _init_ (self, *args, **kwargs ) # Adding a title to the window Tk ) : def _init_ (self, *args, **kwargs ) : In a similar fashion, we are initializing the class through the tk.Tk _init_ as well. This creates a method that runs itself when we form an object from the class. In order to initialize the class, we use the _init_ function. Tk ) :Įxtending from the tk.Tk class allows us to work with components that are present in the Tk() class. # Allowing us to extend from the Tk class class testClass (tk. Let’s create a base class from which we’ll access all the other classes/frames to start off. ![]() The method that we will use for switching between them is the show_frame() method. The Frames we’ll be working with are the MainPage, SidePage and CompletionScreen. This gives the user an illusion that they are being redirected to a different window/tab, but, are really just switching between frames. In order to make it seem like an application with different windows, we’ll also create a function that switches from one frame to another. We first create a root window, on top of which we place a single frame. The way the application works is pretty simple. Let us understand how to work with classes in Tkinter. We’ll also be importing the ttk package separately for ease of use. # Used for styling the GUI from tkinter import tkk # Importing the tkinter module import tkinter as tk In order to work with classes, we’ll need to import the tkinter module. If you’re done with the basic tutorials, let’s get into working with the tkinter module. So before you move ahead, don’t forget to read through the basic tutorials on TKinter here: However, this article will be dealing with a slightly advanced form of the tkinter module, so, it is recommended to go through the beginner series. There’s no need to install any module, as the tkinter module is a part of the standard Python library. There’s a lot of hidden features in tkinter, and one of them is the method to use classes in the module. If you’ve worked with Tkinter before, you probably know that there’s a whole lot of GUI features that you can use to create an application.īut, while creating applications, you quickly realize that there’s more to the module than what meets the eye. Today, we’ll be working with classes in Tkinter.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |