Blog

6/recent/ticker-posts

How to Create Multiple Window Frames with Python's Tkinter Library




 

Creating Multiple Window Frames with Python Tkinter


Tkinter is a powerful library in Python that is used for creating graphical user interfaces (GUIs). One of the most important features of Tkinter is the ability to create multiple window frames, which can be used to organize and display different parts of your application. In this blog post, we will explore how to create multiple window frames with Tkinter and how to use them effectively in your Python applications.

What Is Tkinter?

Tkinter is a Python GUI library that when combined with python provides fast and easy way to create GUI Applications. Tkinter is popular, simple and widely used and also has a ton of references to get you started.

Creating A Window

Let's start by creating a window. It's really quite simple using Tkinter. Let's create a new python file called window.py 

This window will be the parent of all the other frames. The following code creates a new Tkinter application:

PYTHON
from tkinter import *

window = Tk()  # create a window widget
window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
window.state("zoomed")  

window.mainloop()


The window.rowconfigure(0, weight=1) and window.columnconfigure(0, weight=1) lines configure the rows and columns of the main window to have a weight of 1, which allows the window to expand and fill the available space. The window.state('zoomed') line sets the state of the window to "zoomed", which means that the window will take up the entire screen when it is opened.

output: ......



A blank empty window.


Next, three frames are created using the Frame() function, page1, page2, and page3. These frames will be used to display different pages of the application. The frames are then added to the main window using the grid() method. The grid() method is a geometry manager that organizes widgets in a grid-like layout. The for loop and the sticky='nsew' argument ensure that the frames will take up the entire space of the main window.

The show_frame() function is then defined. This function takes in a frame as an argument and raises it to the top of the stacking order using the tkraise() method. This allows the frame to be displayed on top of the other frames and become the active frame.

The code then proceeds to create different widgets (labels, entries, buttons) for each frame and places them in specific coordinates using the place() method. The widgets for page1 include a label for the username, an entry for the username, a label for the password, an entry for the password, and a button for LOGIN. The widgets for page2 include a label for "WELCOME TO PAGE 2" and a button for NEXT. Similarly, the widgets for page3 include a label for "WELCOME TO PAGE 3" and a button for NEXT.

Finally, the show_frame() function is called with page1 as the argument, which sets page1 as the initial active frame when the program runs. The buttons on each frame are also given commands using the command parameter, which call the show_frame() function and display the corresponding frame when clicked. This allows the user to navigate between the different pages of the application.


PYTHON
page1 = Frame(window)
page2 = Frame(window)
page3 = Frame(window)

for frame in (page1, page2, page3):
    frame.grid(row=0, column=0, sticky='nsew')

def show_frame(frame):
    frame.tkraise()

show_frame(page1)

# ============= Page 1 =========
pag1_label = Label(page1, text='Username', font=('Arial', 15, 'bold'))
pag1_label.place(x=50, y=100)

pag1_entry = Entry(page1)
pag1_entry.place(x=170, y=106)

pag1_label2 = Label(page1, text='Password', font=('Arial', 15, 'bold'))
pag1_label2.place(x=50, y=150)

pag1_entry2 = Entry(page1)
pag1_entry2.place(x=170, y=155)

pg1_button = Button(page1, text='LOGIN', font=('Arial', 13, 'bold'), command=lambda: show_frame(page2))
pg1_button.place(x=170, y=200)

# ======== Page 2 ===========
page2.config(background='yellow')
pag2_label = Label(page2, text='WELCOME TO PAGE 2', font=('Arial', 30, 'bold'))
pag2_label.place(x=50, y=100)

pg2_button = Button(page2, text='NEXT', font=('Arial', 13, 'bold'), command=lambda: show_frame(page3))
pg2_button.place(x=190, y=400)

# ======== Page 3 ===========
page3.config(background='gray')
pag3_label = Label(page3, text='WELCOME TO PAGE 3', font=('Arial', 30, 'bold'))
pag3_label.place(x=50, y=100)

pg3_button = Button(page3, text='NEXT', font=('Arial', 13, 'bold'), command=lambda: show_frame(page1))
pg3_button.place(x=190, y=400)




You can download the source code below



Summary

In this article, we looked at how to create a multiple Tkinter window.

My name is Sen Gideons, If you have any questions or comments about this tutorial, please feel free to leave them in the comments section below.





Post a Comment

0 Comments

Ad Code

Responsive Advertisement

Hot Post