create gui for matrix m x n in python

You can create a GUI for a matrix of m x n in Python using the tkinter module. Here's an example code:

main.py
import tkinter as tk

class MatrixGUI:
    def __init__(self, rows, cols):
        self.rows = rows
        self.cols = cols
        
        self.root = tk.Tk()
        
        self.create_widgets()
        
        self.root.mainloop()
        
    def create_widgets(self):
        # Create a frame to hold the matrix
        matrix_frame = tk.Frame(self.root)
        matrix_frame.pack()
        
        # Create the matrix
        for r in range(self.rows):
            for c in range(self.cols):
                cell_label = tk.Label(matrix_frame, text=f"({r+1}, {c+1})")
                cell_label.grid(row=r, column=c, padx=5, pady=5)
        
        # Create a button to exit the program
        exit_button = tk.Button(self.root, text="Exit", command=self.root.quit)
        exit_button.pack(side="bottom", padx=10, pady=10)

# Create a MatrixGUI object with 3 rows and 4 columns
mg = MatrixGUI(3, 4)
914 chars
31 lines

This code creates a GUI with a matrix of size m x n, where m = rows and n = cols. The matrix is created using a tk.Frame widget and tk.Label widgets for each cell. You can customize the appearance of the labels to suit your needs. Finally, there is an exit button that will close the program when clicked.

Note: This example code only creates a static matrix - if you want to create an interactive matrix that users can interact with (e.g. to edit the values), you will need to add additional functionality to the code.

related categories

gistlibby LogSnag