How to pass a 2D array from one class to another in Tkinter?

Tkinter, the standard GUI library for Python, provides a powerful framework for building graphical user interfaces. When developing complex applications, it often becomes necessary to pass data between different classes or components. In this article, we will explore the process of passing a 2D array from one class to another in Tkinter with three different approaches.

Consider a common scenario where we have a Tkinter application with two classes: ClassA and ClassB. ClassA contains a 2D array, and we want to pass this array to ClassB to perform operations or display the data.

Approach 1: Passing Through Constructor

The most straightforward way is to pass the 2D array through the constructor of ClassB ?

import tkinter as tk

class ClassA:
    def __init__(self):
        self.data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        self.open_class_b()

    def open_class_b(self):
        ClassB(self.data)

class ClassB:
    def __init__(self, data):
        self.data = data
        self.display_data()

    def display_data(self):
        print("2D Array:", self.data)

root = tk.Tk()
app = ClassA()
root.mainloop()
2D Array: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In this approach, ClassA creates an instance of ClassB and passes the 2D array directly through the constructor. ClassB receives the array as a parameter and stores it in its own data attribute.

Approach 2: Using a Setter Method

This approach uses a setter method in ClassB to receive the 2D array from ClassA ?

import tkinter as tk

class ClassA:
    def __init__(self):
        self.data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        self.open_class_b()

    def open_class_b(self):
        class_b_instance = ClassB()
        class_b_instance.set_data(self.data)

class ClassB:
    def __init__(self):
        self.data = None

    def set_data(self, data):
        self.data = data
        self.display_data()

    def display_data(self):
        print("2D Array:", self.data)

root = tk.Tk()
app = ClassA()
root.mainloop()
2D Array: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Here, ClassA creates an instance of ClassB and calls the set_data method to pass the array. This approach provides more flexibility as you can set the data after object creation.

Approach 3: Using a Shared Data Structure

This approach uses a shared data structure that both classes can access ?

import tkinter as tk

class SharedData:
    def __init__(self):
        self.data = None

    def set_data(self, data):
        self.data = data

    def get_data(self):
        return self.data

class ClassA:
    def __init__(self, shared_data):
        self.shared_data = shared_data
        self.data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        self.shared_data.set_data(self.data)
        self.open_class_b()

    def open_class_b(self):
        class_b_instance = ClassB(self.shared_data)
        class_b_instance.display_data()

class ClassB:
    def __init__(self, shared_data):
        self.shared_data = shared_data

    def display_data(self):
        data = self.shared_data.get_data()
        print("2D Array:", data)

root = tk.Tk()
shared_data = SharedData()
app = ClassA(shared_data)
root.mainloop()
2D Array: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

This approach introduces a SharedData class that acts as a centralized data store. Both ClassA and ClassB receive a reference to the same SharedData instance, allowing them to share data through set_data and get_data methods.

Comparison

Approach Best For Advantages Disadvantages
Constructor Simple data transfer Direct, efficient Data must be available at creation
Setter Method Flexible timing Can set data anytime Requires method call
Shared Structure Multiple classes accessing same data Centralized data management More complex, potential coupling

Conclusion

These three approaches provide different ways to pass 2D arrays between Tkinter classes. Choose the constructor approach for simple scenarios, setter methods for flexible timing, or shared data structures when multiple classes need access to the same data.

Updated on: 2026-03-27T16:08:19+05:30

353 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements