LoginSignup
3
4

More than 3 years have passed since last update.

PythonGUI(テキストボックス)で行列を作成

Last updated at Posted at 2020-01-13

GUI(tkinterテキストボックス)で行列の要素の値を入力し2次元配列型のデータを取得するソースです。行列は、変数により任意のサイズに変更可能。

qiita.rb
from tkinter import *
from tkinter import ttk
import numpy as np

#入力用のGUI
def GUI_Input(n,m):

    root = Tk()
    root.title('Table Input')

    #入力用フレーム
    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):


            list_Items[k] = ttk.Entry(frame,width=2)
            list_Items[k].grid(row=i+1, column=j+1)
            k+=1


    #テキストボックスからデータを取得し2次元配列としてprint出力
    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)




    #実行ボタンの設置
    button_Run = ttk.Button(root,
                            text='実行',
                            padding=5,
                            command=ButtonClicked_Run)
    button_Run.grid(row=1, column=0)

    root.mainloop()


#n,mの数を変えて、表の行数を変える

m = 9
n = 9
GUI_Input(m,n)

結果
           スクリーンショット 2020-01-13 23.27.57.png

                   ↓
            スクリーンショット 2020-01-13 23.28.37.png

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4