Delrius Euphoria
Delrius Euphoria

Reputation: 15088

Building matrix GUI with tkinter

So here in tkinter i have managed to make a gui that takes input and prints(in terminal) the matrix out. But im not able to properly iterate through the items of the matrix and hence print the matrix correctly. So i wanted to know if there is some other approach to this or a way to fix this problem.

Snippet:

from tkinter import *
import numpy as np

root = Tk()

def process():
    values = [e1.get(),e2.get(),e3.get(),e4.get()]

    a = np.zeros((2,2),dtype=np.int64)
    
    for i in range(2):
        for j in range(2):
            for value in values:
                x = value
                a[i][j] = x
    print(a)

e1 = Entry(root)
e2 = Entry(root)
e3 = Entry(root)
e4 = Entry(root)

e1.grid(row=0,column=0,padx=10,pady=10)
e2.grid(row=0,column=1)
e3.grid(row=1,column=0,padx=10,pady=10)
e4.grid(row=1,column=1)

b = Button(root,text='Process',command=process)
b.grid(row=2,column=0,columnspan=4,sticky=E+W)

root.mainloop()

When i print out the matrix im getting a matrix but all the elements are the value of e4.get(). I know its cause of the mistake in iteration, is there a way to get the proper matrix printed out. and also the code is right now not flexible and only works for 2x2 matrix, but i need the solution to this to continue :(

Thanks in advance :)

Upvotes: 0

Views: 1013

Answers (2)

jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

Another way to divide them without for loop:

def process():
    values = iter([int(e1.get()), int(e2.get()), int(e3.get()), int(e4.get())])
    matrix = np.array(list(zip(*[values]*2)))
    print(matrix)

Upvotes: 2

acw1668
acw1668

Reputation: 46669

You should not use the for value in values: loop:

for i in range(2):
    for j in range(2):
        a[i][j] = values[i*2+j]

Upvotes: 3

Related Questions