It is a source to input the values of matrix elements with GUI (tkinter combo box) and acquire 2D array type data. The size of the matrix and the list of combo boxes can also be changed by variables.
qiita.rb
from tkinter import *
from tkinter import ttk
import numpy as np
#GUI for input
def GUI_Input(n,m):
root = Tk()
root.title('Table Input')
#Input frame
frame = ttk.Frame(root)
frame.grid(row=0, column=0)
list_Items = [0]*(n*m)
N = n
M = m
k=0
for i in range(0, n):
for j in range(0, m):
valuelist = [1,2,3,4,5,6,7,8,9]
list_Items[k] = ttk.Combobox(frame,values=valuelist,width = 2)
list_Items[k].grid(row=i+1, column=j+1)
k+=1
#Get data from combo box and output pritnt as a two-dimensional array
def ButtonClicked_Run():
B = [0]*(N*M)
for i in range(N*M):
B[i] = list_Items[i].get()
A= np.reshape(B, (N,M))
print(A)
#Installation of execute button
button_Run = ttk.Button(root,
text='Run',
padding=5,
command=ButtonClicked_Run)
button_Run.grid(row=1, column=0)
root.mainloop()
#Specify the size of the matrix by changing the number of n and m
m = 9
n = 9
GUI_Input(m,n)
result ⬇︎
Recommended Posts