Tkinter's toplevel widget is a powerful tool for creating independent windows in your Python GUI applications. It allows you to create new windows that can be placed over or alongside the main application window, enhancing interactivity and user experience. Let's dive into an example that demonstrates the use of Tkinter's toplevel widget.

In this example, we will create a simple to-do list application with a main window and a toplevel window for adding tasks. The main window will display the list of tasks, and the toplevel window will provide an interface for adding new tasks to the list.

Setting Up the Main Application Window
The main application window will be created using the Tk class from the tkinter module. This window will hold our to-do list, which is initially empty.

We will also create a function called add_task that will open the toplevel window when called. This function will be attached to a button in the main window, allowing users to open the toplevel window whenever they want to add a new task.
Creating the Main Window

First, let's create the main application window and display it on the screen. We'll use a simple list to store our tasks and a Label widget to display the list.
```python import tkinter as tk tasks = [] def main_window(): root = tk.Tk() root.title("To-Do List") root.geometry("300x400") list_tasks = tk.Listbox(root, width=50, height=10) list_tasks.pack(pady=10) for task in tasks: list_tasks.insert(tk.END, task) btn_add_task = tk.Button(root, text="Add Task", command=add_task) btn_add_task.pack(pady=10) root.mainloop() main_window() ```
In this code, we create a Tk instance as the main window and set its title and dimensions. Then, we create a Listbox widget to display our tasks. We loop through the tasks list and insert each task into the Listbox. Finally, we create a Button widget linked to the add_task function and pack it into the main window.
Creating the Toplevel Window for Adding Tasks

Now that we have our main window set up, let's create the toplevel window for adding new tasks to our to-do list.
The toplevel window will contain an Entry widget for users to input their task, and a Button widget to submit the task. When the task is submitted, we will add it to the tasks list and update both the main window and the toplevel window to reflect the changes.
Creating the Toplevel Window

Let's create the toplevel window and add the necessary widgets to it.
```python def add_task(): task_window = tk.Toplevel(root) task_window.title("Add Task") lbl_task = tk.Label(task_window, text="Task:") lbl_task.pack(pady=10) entry_task = tk.Entry(task_window, width=50) entry_task.pack(pady=10) def submit_task(): task = entry_task.get() tasks.append(task) list_tasks.insert(tk.END, task) task_window.destroy() btn_submit = tk.Button(task_window, text="Submit", command=submit_task) btn_submit.pack(pady=10) ```
In this code, we create a Toplevel instance as the toplevel window and set its title. We then create a Label and an Entry widget for users to input their task. We also create a Button widget linked to the submit_task function, which adds the task to the tasks list, updates the main window, and destroys the toplevel window.




















Running this code will create a simple to-do list application with a main window for viewing tasks and a toplevel window for adding new tasks. Users can click the "Add Task" button in the main window to open the toplevel window and input a new task. Once they submit the task, it will be added to the to-do list in both windows.
Now that you have seen an example of using Tkinter's toplevel widget, you can explore and experiment with other Tkinter widgets and methods to create more advanced GUI applications. Happy coding!