我希望用户在tkinter GUI中输入矩阵。当用户按下“提交”时,它将显示输入的矩阵。我知道如何以正常方式输入矩阵,但是不知道如何在“ tkinter”中进行输入。只是告诉我如何在tkinter中定义矩阵,在Entry中写什么代替“ 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()
为此使用数组。
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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句