How to make tkinter frames in a loop and update object values?


In this tutorial, we will explore the dynamic creation of Tkinter frames in a loop and the real-time update of object values within these frames. We'll walk through a practical example – building a dynamic task manager that allows users to manage a list of tasks with toggling statuses.

Setting up Tkinter and Task Class

Before diving into the main functionality, ensure Tkinter is installed −

pip install tk

Let's define a simple Task class representing a task with a name and a status −

import tkinter as tk

class Task:
   def __init__(self, name, status):
      self.name = name
      self.status = status

Dynamic Task Manager GUI

Now, let's create a dynamic task manager that generates frames for each task in the list, displaying task details and a button to toggle the status −

# Sample list of tasks
tasks = [
   Task("Task 1", "Incomplete"),
   Task("Task 2", "Incomplete"),
   Task("Task 3", "Incomplete"),
]

# Update function
def update_status(task, label):
   if task.status == "Incomplete":
      task.status = "Complete"
   else:
      task.status = "Incomplete"
   label.config(text=f"Status: {task.status}")

# Main function to create Tkinter GUI
def create_task_manager():
   root = tk.Tk()
   root.title("Tkinter Frames in Loop")
   root.geometry("720x300")

   # Create frames, labels, and buttons dynamically
   frames = []
   for task in tasks:
      frame = tk.Frame(root, padx=10, pady=10)
      frame.pack(fill=tk.X)

      # Label to display task name
      name_label = tk.Label(frame, text=f"Task: {task.name}")
      name_label.pack(anchor=tk.W)

      # Label to display task status
      status_label = tk.Label(frame, text=f"Status: {task.status}")
      status_label.pack(anchor=tk.W)

      # Button to update task status
      update_button = tk.Button(
         frame, text="Toggle Status", 
         command=lambda task=task, label=status_label: update_status(task, label)
      )
      update_button.pack(anchor=tk.E)
      frames.append(frame)

   root.mainloop()

# Run the task manager GUI
if __name__ == "__main__":
   create_task_manager()

Understanding the Code

  • We define a Task class with attributes for the task name and status.

  • A sample list of tasks (tasks) is created with initial statuses.

  • The update_status function toggles the task status when called.

  • In the create_task_manager function, frames, labels, and buttons are dynamically created for each task. The update_button is associated with the update_status function.

  • Finally, the Tkinter window is launched with the root.mainloop().

Running the Task Manager

Save the code in a file (e.g., dynamic_task_manager.py) and execute it. A Tkinter window will appear, displaying frames for each task with task names, statuses, and "Toggle Status" buttons. Clicking the buttons will dynamically update the task statuses.

Putting It All Together

Now let’s combine all the steps and check the final output −

Example

import tkinter as tk

class Task:
   def __init__(self, name, status):
      self.name = name
      self.status = status

# Sample list of tasks
tasks = [
   Task("Task 1", "Incomplete"),
   Task("Task 2", "Incomplete"),
   Task("Task 3", "Incomplete"),
]

# Update function
def update_status(task, label):
   if task.status == "Incomplete":
      task.status = "Complete"
   else:
      task.status = "Incomplete"
   label.config(text=f"Status: {task.status}")

# Main function to create Tkinter GUI
def create_task_manager():
   root = tk.Tk()
   root.title("Tkinter Frames in Loop")
   root.geometry("720x300")

   # Create frames, labels, and buttons dynamically
   frames = []
   for task in tasks:
      frame = tk.Frame(root, padx=10, pady=10)
      frame.pack(fill=tk.X)

      # Label to display task name
      name_label = tk.Label(frame, text=f"Task: {task.name}")
      name_label.pack(anchor=tk.W)

      # Label to display task status
      status_label = tk.Label(frame, text=f"Status: {task.status}")
      status_label.pack(anchor=tk.W)

      # Button to update task status
      update_button = tk.Button(
         frame, text="Toggle Status",
         command=lambda task=task, label=status_label: update_status(task, label)
      )
      update_button.pack(anchor=tk.E)

      frames.append(frame)

   root.mainloop()

# Run the task manager GUI
if __name__ == "__main__":
   create_task_manager()

Output

The above code has a Task class representing a task with a name and a status ("Incomplete" or "Complete"). The GUI dynamically creates frames for each task, displaying the task name, status, and a button to toggle the status.

Conclusion

In conclusion, this article has illustrated the dynamic capabilities of Tkinter in creating a responsive task manager. By using dynamic frames and real-time object updates, developers can craft interactive interfaces for various applications.

Updated on: 15-Feb-2024

10 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements