Search code examples
pythonuser-interfacetkintermatrixtkinter-entry

How to change values from a matrix GUI tkinter?


I want to change a number from a matrix and then display it in the same tk window, but I find it hard to work with variables from an input. The r[][] should be the matrix formed with the user's input. And after all I have to display the matrix with the modification: r[0][1] += 5, in the same tk window.

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):
                a[i][j] = values[i*2+j]
    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()

r=[[e1.get(),e2.get()],[e3.get(),e4.get()]]
r[0][1] += 5 

Solution

  • Would this be what you're looking for? I deleted a bunch of code that seems to do nothing in context -- you just want to replace the text in the corner box right?

    from tkinter import *
    
    def process():
        replace(e4)
    
    def replace(entry_loc):
        temp = int(entry_loc.get())
        temp += 5
        entry_loc.delete(0,500)
        entry_loc.insert(0, temp)
        
    root = Tk()
    
    var_e1 = StringVar
    var_e2 = StringVar
    var_e3 = StringVar
    var_e4 = StringVar
    e1 = Entry(root, textvariable=var_e1)
    e2 = Entry(root, textvariable=var_e2)
    e3 = Entry(root, textvariable=var_e3)
    e4 = Entry(root, textvariable=var_e4)
    
    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()