1

Can someone please help me, I'm kinda stuck in this part cause I'm still familiarizing myself. I wanna display my 2d list in the "Your Array" Label widget.

Sample Pic

how can I possibly do that? Here's my incomplete code:

from tkinter import *
from random import randint


arraylist = [[0] * 10 for i in range(10)]


def generate():
    r = int(numrow.get())
    c = int(numcol.get())
    Label(main, text="Your Array:").grid(row=2)
    blank = Text(main, width=75, height=7)
    blank.delete(0.0, END)

    for row in range(r):
        for column in range(c):
            arraylist[row][column] = randint(1, 100)
    for x in range(r):
        for y in range(c):
            print('\t', arraylist[x][y], end=' ')
            blank.insert(END,"%d"%(arraylist[x][y]))
            blank.grid(row=x+2, column=y+1, sticky=NSEW)

            print()

main = Tk()
main.geometry("500x500")
Label(main, text="Enter Row:").grid(row=0)
Label(main, text="Enter Column:").grid(row=1)

numrow = Entry(main)
numcol = Entry(main)

numrow.grid(row=0, column=1)
numcol.grid(row=1, column=1)

Button(main, text='Quit', command=main.destroy).grid(row=4, column=0, sticky=W, pady=4)
Button(main, text='Show', command=generate).grid(row=4, column=1, sticky=W, pady=4) 

mainloop()
Edouard Thiel
  • 5,878
  • 25
  • 33
WMCA
  • 13
  • 4
  • 1
    Try this approach: [Pretty print data in tkinter Label](https://stackoverflow.com/a/58660344/7414759) – stovfl Nov 07 '19 at 14:37
  • 1
    if you use `\t` and `\n` in `print()` then why you don't use `\t` and `\n` in `blank.insert()` ? – furas Nov 07 '19 at 15:31
  • if you want to put every value in separated `Text` then you have to create `blank = Text()` inside `for`-loops. Currenty you has only one `Text` and you always put values in the same `Text`. – furas Nov 07 '19 at 15:34

1 Answers1

2

If you want to put all in one Text then you should insert \t, \n and spaces in Text like you do it with print()

blank.insert(END, "\t%d  " % (value))

blank.insert(END, "\n")

def generate():
    r = int(numrow.get())
    c = int(numcol.get())

    Label(main, text="Your Array:").grid(row=2)

    blank = Text(main, width=75, height=7)
    blank.delete(0.0, END)
    blank.grid(row=2, column=1, sticky=NSEW)

    for x in range(r):
        for y in range(c):
            value = randint(1, 100)
            arraylist[x][y] = value

            print('\t', value, end=' ')
            blank.insert(END, "\t%d  " % (value))

        print()
        blank.insert(END, "\n")

if you want every value in separated Text then you have to create blank inside for-loop

def generate():
    r = int(numrow.get())
    c = int(numcol.get())

    Label(main, text="Your Array:").grid(row=2)

    for x in range(r):
        for y in range(c):

            blank = Text(main, width=3, height=1)
            blank.grid(row=x+2, column=y+1, sticky='NSEW')

            value = randint(1, 100)
            arraylist[x][y] = value

            print('\t', value, end=' ')
            blank.insert(END, "%d" % (value))

        print()
furas
  • 134,197
  • 12
  • 106
  • 148