用于矩阵的Tkinter动态字典-不能在输入窗口上使用.get方法



我一直在开发一个程序,该程序可以生成用户定义大小的矩阵,然后可以对矩阵执行操作。这个程序分为几个部分。

首先,用户输入行和列,提交后将打开一个带有输入框矩阵的tkinter窗口。

这些条目框中的每一个都是动态entries字典中的一个值。列表gridrowsgridcols用于给出用于排列输入框的.grid方法的坐标。

我遇到的问题是,在将用户的值键入矩阵后,分配一个命令来获得用户输入。我已经尝试创建一个函数storevals,它将把所有矩阵条目附加到matrixinput列表中。

当我运行这个程序时,我会得到矩阵输入窗口,在矩阵的每个框中键入值后,我会从第50行得到相同的错误消息:AttributeError:"NoneType" object has no attribute "get"。输入框似乎没有类型,这似乎在.grid排列窗口后丢失。

有没有办法将这些窗口中的用户条目分配到列表中?任何帮助都将不胜感激。

谢谢!

from matplotlib import pyplot as plt
from tkinter import *
#Take user input for matrix size and store in matrixrows and matrixcols variables as integers.
master = Tk()
master.title("Matrix Size")
Label(master, text = "Rows").grid(row=0)
Label(master, text = "Columns").grid(row=1)

def storevals():
matrixrows.append(int(rows.get()))
matrixcols.append(int(cols.get()))
master.quit()
matrixrows = []
matrixcols = []

rows = Entry(master)
cols = Entry(master)
rows.grid(row = 0, column = 1)
cols.grid(row =1, column = 1)
Button(master, text= "Enter", command = storevals).grid(row=3, column =0, sticky=W, pady=4)
mainloop()
norows = matrixrows[0]
nocols = matrixcols[0]
matrixlist =[]
for i in range(0,norows):
matrixlist.append([])
for i in range(0,norows):
for j in range(0,nocols):
matrixlist[i].append(nocols*0)

#Generate a matrix entry window with entries correpsonding to the matrix
master2 = Tk()
master2.title("Enter Matrix Values")
matrixinput=[]
def storevals():
for key in entries:
matrixinput.append(entries[key].get())
print(matrixinput)

Button(master2, text= "Submit Matrix", command = storevals).grid(row=norows+1, column =round(nocols/2), sticky=W, pady=4)
gridrows=[]
gridcols=[]

#creates two lists, gridcols and gridrows which are used to align the entry boxes in the tkinter window
for i in range(0,norows):
for j in range(0,nocols):
gridrows.append(i)
for i in range(0,norows):
for j in range(0,nocols):
gridcols.append(j)

entries ={}
for x in range(0,nocols*norows):
entries["Entrybox{0}".format(x)]=Entry(master2).grid(row=gridrows[x], column=gridcols[x])
print(entries)
mainloop()

您可能还没有查看在mainloop((之前打印的条目的内容

entries ={}
for x in range(0,nocols*norows):
entries["Entrybox{0}".format(x)]=Entry(master2).grid(row=gridrows[x], column=gridcols[x])
print(entries)

grid()pack()place()都返回None

如果您想保留小部件,您需要将其分配给一个变量,然后调用grid()

entries ={}
for x in range(0,nocols*norows):
widget = Entry(master2)
widget.grid(row=gridrows[x], column=gridcols[x])
entries["Entrybox{0}".format(x)] = widget
print(entries)

最新更新