Python 如何在tkinter中输入矩阵?

Python 如何在tkinter中输入矩阵?,python,python-3.x,matrix,tkinter,Python,Python 3.x,Matrix,Tkinter,我希望用户在tkinter GUI中输入矩阵。当用户按下“提交”键时,应显示输入的矩阵。我知道如何以正常的方式输入矩阵,但不知道如何在“tkinter”中输入矩阵。 请告诉我如何在tkinter中定义矩阵,在条目中用什么来代替“textvariable” 这是我的密码: from tkinter import * window = Tk() window.title("Matrix") window.geometry("650x500+120+120") window.configure(bg=

我希望用户在tkinter GUI中输入矩阵。当用户按下“提交”键时,应显示输入的矩阵。我知道如何以正常的方式输入矩阵,但不知道如何在“tkinter”中输入矩阵。 请告诉我如何在tkinter中定义矩阵,在条目中用什么来代替“textvariable”

这是我的密码:

from tkinter import *
window = Tk()
window.title("Matrix")
window.geometry("650x500+120+120")
window.configure(bg='bisque2')
window.resizable(False, False)
m1 = StringVar()

Label(window, text="Enter matrix :", font=('arial', 10, 'bold'), 
bg="bisque2").place(x=20, y=20)

x2 = 0
y2 = 0
rows, cols = (3,3)
for i in range(rows):
  for j in range(cols):

        entry = Entry(window, textvariable =  ,width=3)
        entry.place(x=60 + x2, y=50 + y2)
        x2 += 30

  y2 += 30
  x2 = 0
button= Button(window,text="Submit", bg='bisque3', width=15)
button.place(x=160,y=140)
window.mainloop()

您可以为此使用
StringVar()
s数组

from tkinter import Tk, Label, StringVar, Button, Entry
window = Tk()
window.title("Matrix")
window.geometry("650x500+120+120")
window.configure(bg='bisque2')
window.resizable(False, False)

# empty arrays for your Entrys and StringVars
text_var = []
entries = []

# callback function to get your StringVars
def get_mat():
    matrix = []
    for i in range(rows):
        matrix.append([])
        for j in range(cols):
            matrix[i].append(text_var[i][j].get())

    print(matrix)

Label(window, text="Enter matrix :", font=('arial', 10, 'bold'), 
      bg="bisque2").place(x=20, y=20)

x2 = 0
y2 = 0
rows, cols = (3,3)
for i in range(rows):
    # append an empty list to your two arrays
    # so you can append to those later
    text_var.append([])
    entries.append([])
    for j in range(cols):
        # append your StringVar and Entry
        text_var[i].append(StringVar())
        entries[i].append(Entry(window, textvariable=text_var[i][j],width=3))
        entries[i][j].place(x=60 + x2, y=50 + y2)
        x2 += 30

    y2 += 30
    x2 = 0
button= Button(window,text="Submit", bg='bisque3', width=15, command=get_mat)
button.place(x=160,y=140)

window.mainloop()