Home > Back-end >  How can i create a matrix from user's input in dialog box using tkinter
How can i create a matrix from user's input in dialog box using tkinter

Time:02-28

I have a 19x5 matrix that's by default only zeros. I want to create a window using tkinter that displays an empty 19x5 matrix that the user will fill with values (positive real) or leave blank. In that case i want blank inputs to remain zero and input values to replace the zeros in the respective place and the new matrix is saved.

import numpy as np
import PySimpleGUI as sg
from tkinter import *

demand = np.zeros((19,5))

CodePudding user response:

tkinter has Entry() to get text, and you can use it with layout manager .grid(row, column) to create matrix/table with many Entry(). You can use two for-loops to put widgets in rows and columns.

But you can do it in standard window - tkinter.Tk() - not in dialog box.

You have to add code which gets values from all Entry() and put in numpy array. It is better to assign this code to Button().

enter image description here

import numpy as np
import tkinter as tk   # PEP8: `import *` is not preferred

# --- functions ---

def get_data():
    for r, row in enumerate(all_entries):
        for c, entry in enumerate(row):
            text = entry.get()
            demand[r,c] = float(text)
        
    print(demand)
    
# --- main ---

rows = 19
cols = 5

demand = np.zeros((rows, cols))

window = tk.Tk()

all_entries = []
for r in range(rows):
    entries_row = []
    for c in range(cols):
        e = tk.Entry(window, width=5)  # 5 chars
        e.insert('end', 0)
        e.grid(row=r, column=c)
        entries_row.append(e)
    all_entries.append(entries_row)
        
b = tk.Button(window, text='GET DATA', command=get_data)
b.grid(row=rows 1, column=0, columnspan=cols)

window.mainloop()        

EDIT:

You could also use Label to add numbers for rows and cols

enter image description here

import numpy as np
import tkinter as tk   # PEP8: `import *` is not preferred

# --- functions ---

def get_data():
    for r, row in enumerate(all_entries):
        for c, entry in enumerate(row):
            text = entry.get()
            demand[r,c] = float(text)
        
    print(demand)
    
# --- main ---

rows = 19
cols = 5

demand = np.zeros((rows, cols))

window = tk.Tk()

for c in range(cols):
    l = tk.Label(window, text=str(c))
    l.grid(row=0, column=c 1)

all_entries = []
for r in range(rows):
    entries_row = []
    l = tk.Label(window, text=str(r 1))
    l.grid(row=r 1, column=0)
    for c in range(cols):
        e = tk.Entry(window, width=5)  # 5 chars
        e.insert('end', 0)
        e.grid(row=r 1, column=c 1)
        entries_row.append(e)
    all_entries.append(entries_row)
        
b = tk.Button(window, text='GET DATA', command=get_data)
b.grid(row=rows 1, column=0, columnspan=cols)

window.mainloop()        

If you want something more complex (with more functions) then you could use pandastable which you can see in DataExplorer

More in my post on blog: Tkinter PandasTable Examples


BTW: PEP 8 -- Style Guide for Python Code

  • Related