Tkinter MatrixCant动态字典-在输入窗口上使用.get方法

时间:2018-07-18 21:35:51

标签: python loops matrix tkinter

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

首先,用户输入行和列,提交后,将打开带有输入框矩阵的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()

1 个答案:

答案 0 :(得分:0)

您可能没有看过在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)