1
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

PythonGUI(tkinterコンボボックス)で行列を作成

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):


            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


    #コンボボックスからデータを取得し2次元配列としてpritnt出力
    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.25.22.png
             ⬇︎
      スクリーンショット 2020-01-13 23.26.03.png

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
1
Help us understand the problem. What are the problem?