In this Python Tkinter Tutorial, we will discuss the usage and inner workings behind the “mainloop” function.
We use this function on our Tkinter window, typically called “root”, in every single Tkinter program. But often the actual working and purpose of this function is forgotten. This tutorial aims to explain this, so you have a better idea of what’s going on behind the scenes.
Understanding Tkinter MainLoop
If you remember correctly, the mainloop function is called as shown below. And until it is called, the Tkinter window will not appear.
import tkinter as tk root = tk.Tk() root.mainloop()
Think about how code is executed for a moment. Normally, your programs will begin executing and finish within a fraction of a second. But this does not happened with games or GUI windows, which last indefinetly. Have you ever wondered why?
This is because they must run infinitely, until they are ordered to be closed, either by the program or by a manual action by the user. So how is this possible? With Loops of course. With the right condition, a loop can run indefinitely, repeating the same chunk of code over and over again.
If we take a deeper look into this, there are several more elements to it. There is what we call an “event loop” (within the infinite loop) that “listens” for certain actions that the user may take (such as clicking a button). Once an event has been detected, a corresponding action is taken (such as closing the window when the quite button is pressed)
Without an event loop, GUI windows would remain static, and unable to change or be interactive like they normally are. You may not realize it, but Tkinter has one of these too.
If you want to visualize the Tkinter MainLoop function a bit, the below code should give you a little idea.
while True: event = wait_for_event() event.process() if main_window_has_been_destroyed(): break
Game Loop in Pygame
In order to better understand the Tkinter
MainLoop() function, let’s take a look at another popular Python Library called Pygame.
Pygame is a game library used to create simple 2D games in Python. And as I said earlier, games also run infinitely, hence the also need an infinite loop, commonly referred to as the game loop.
One big difference between Tkinter and Pygame, is that you have to make your own (infinite) Game loop in Pygame. This actually helps build up your understanding alot, and makes things much more flexible and under your control.
Shown below is the code for a Game Loop in Pygame. You don’t need to focus on the syntax much, rather just o the concept.
entities = pygame.sprite.Group() entities.add(Player) entities.add(Enemy) while True: # Event Loop for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_M: print("M-key was pressed") # Updating for entity in entities: entity.update() # Rendering display.render(background) for entity in entities: display.render(entity) pygame.display.update()
A brief description of some important elements in the above code:
- There is an infinite while loop, that only breaks once the QUIT event is detected.
- Within the While loop, there is a for loop that we call the event loop. (Implementation will vary from library to library, but all have an event loop that continuously listens for events, and then acts accordingly)
- Update function is being called on all entities in every iteration of the loop.
- All entities are re-drawn to the screen in every iteration of the loop.
These features can be said to be very similar to those found within the Tkinter mainloop. Hence it should serve as a good reference.
This marks the end of the Python Tkinter MainLoop Function Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.