1

I've been playing around with tkinter and loving what I can do. I'm a total noob, however, and wanting to make my own matrix multiplier I've run into numerous problems. My goal is to create two grids of tk entries and be able to call them back later with .get, when I call txt_ent_a.get() I get an error: list has no attribute 'get'.

I found these other questions that have helped me get this far:

List of lists changes reflected across sublists unexpectedly

How can I create a list of entries in tkinter

tkinter: Button instance has no attribute 'get'

And this one that is basically the same as my question, though I didn't really understand how to take the conceptual answer and convert it into code:

Saving variables in 'n' Entry widgets Tkinter interface

I know what I have created as "entry_list" is just an empty list of lists. My real question is: how do I loop through making entry widgets and key or objects (I'm shaky on the lingo) that I can loop through using .get() on later, which I can then make into a list of lists (a matrix) that I can use in a function?

Because I'm so new to Python, I'm not sure if I just need to add a StringVar somehow, or .pack something somehow... I believe .pack is a tkinter thing. I'll try and keep my code cleanish, thank you for any help.

from tkinter import *
import tkinter as tk
window = Tk()
lbl1 = Label(window, text="# of rows: 3").grid(column=0, row=0)
lbl2 = Label(window, text="# of cols: 2").grid(column=0, row=1)
window.geometry('250x250')
def clickedc():     #create matrix click event
    row =  (3)      #In final version these will be the numbers I loop over when 
    col =  (2)      #making the entries in question.
    lblA = Label(window, text="Input matrix:").grid(column=(int(col/2)), row=3)
    entry_list = [] #This is what I want to be a list I can .get() from later
    r = 1
    for r in range(int(row)):
        entry_list.append([])
        c = 1       
        for c in range(int(col)):
            entry_list[-1].append(tk.Entry(window, width=10).grid(column=c, row=4 + r))

    def clickeds():         #Solving click event
        r = 1
        matrix_A = []       #The matrix I want to put the values into, for reference
        for r in range(int(row)):
            c = 1
            for c in range(int(col)):
                matrix_A[r - 1:c - 1] = entry_list.get("1.0",'end-1c') #Causes Error   
            #Here I'll: output a new matrix as a grid of labels.ex: #1  1
    btn_s = Button(window, text="Solve", command=clickeds)          #1  0
    btn_s.grid(column=int(col) + 1, row=3)                          #0  1

btn_c = Button(window, text="Create", command=clickedc)
btn_c.grid(column=3, row=1)

window.mainloop()

I hope this code is clear enough. Any advice is welcome, I assume I code rather poorly.

McKay L
  • 25
  • 7
  • You are assuming `list` is something that it is not. The first step in debugging is to validate your assumptions. Also, the code is indeed too long. If the problem is with a widget or list, you don't need a dozen widgets to illustrate the problem. – Bryan Oakley Jun 15 '18 at 02:23
  • Thanks Bryan, I'll shorten it. When I print the list it gives me a list of lists filled with None: [[None, None] , [None, None]]. Am I approaching this wrong, maybe? Is it better to use a different variable, or use a whole different type of GUI to begin with? Anyways, thank you. – McKay L Jun 15 '18 at 04:45
  • Your code is not testable. There are variables that have not been defined. Please reduce your code down to a simple example. Take a look at this link [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Mike - SMT Jun 15 '18 at 11:46
  • Thanks for the tip! – McKay L Jun 15 '18 at 15:11
  • I have improved the readability of the code (hopefully), and it actually works now if you copy paste it. I hope it's short enough. – McKay L Jun 15 '18 at 20:14
  • The error is telling you the exact problem: `'list' object has no attribute 'get'`. In other words, you can't call a list's `get` function, because it doesn't have one. I think what you mean to do is get the text from each `Entry` in `entry_list`, but that is not what you are doing. Please clarify **exactly** what each line of code should do. – Artemis Jun 16 '18 at 16:10

0 Answers0