-1

Sorry if the title is confusing I don't really know how to word it properly. Hopefully the code below will clear it up.

When I run the program and I call print_names(), only the last name "Jenny" shows up.

While if I call the other two functions, all the items in the corresponding list show up.

However unless I add 'row=' then the different functions print on separate rows.

screenshot

I want to add row= so I can stop the different functions printing below each other, but if I add it, it causes only the last name to be printed.

How do I stop this from happening?

from tkinter import *
from tkinter.font import BOLD

gui = Tk()
jnames = [['Jamie','25','Male'],['John','22','Male'],['Jenny','23','Female']]

def quit():
    gui.quit()

def print_names():
    for x in jnames:
        Label(gui, text=((x[0]))).grid(column=0, row=3)

def print_ages():
    for x in jnames:
        Label(gui, text=((x[1]))).grid(column=1)

def print_gender():
    for x in jnames:
        Label(gui, text=((x[2]))).grid(column=2)

def main():
    Button(gui, text='Quit', command=quit).grid(column=1, row=0, sticky=EW)
    Button(gui, text='Print names', command=print_names).grid(column=0, row=1, sticky=EW)
    Button(gui, text='Print age', command=print_ages).grid(column=1, row=1, sticky=EW)
    Button(gui, text='Print gender', command=print_gender).grid(column=2, row=1, sticky=EW)
    Label(gui, text='Name', font=BOLD, width=15).grid(column=0, row=2, sticky=EW)
    Label(gui, text='Age', font=BOLD, width=15).grid(column=1, row=2, sticky=EW)
    Label(gui, text='Gender', font=BOLD, width=15).grid(column=2, row=2, sticky=EW)
    gui.mainloop()
main()
Michael S.
  • 3,050
  • 4
  • 19
  • 34
  • You're putting all the names in the same row and column. How do you expect to see them all? The other functions don't force the labels into the same row, so they append to the grid. – Barmar Aug 30 '22 at 23:48
  • Yes I know, but I don't know how to fix it, I have only been coding for a couple days. – haoisdfoas Aug 30 '22 at 23:52
  • Have you tried experimenting with different row and column numbers? – Bryan Oakley Aug 30 '22 at 23:54
  • Why don't you just leave out the row argument, like you do in the other functions? Then it will lay things out automatically. – Barmar Aug 30 '22 at 23:56
  • As you can see in the screenshot if I leave out the row argument, for some reason each function is below the previous one. – haoisdfoas Aug 30 '22 at 23:58

2 Answers2

0

You can generate a string with all the names (or ages or genders) and then place this in the desired column and row. Here is an example:

def print_names():
    names = [x[0] for x in jnames]
    Label(gui, text='\n'.join(names)).grid(column=0, row=3)

The above will simply create a multi-line string and add it to column 0 and row 3.

Prins
  • 1,051
  • 1
  • 6
  • 9
0

Try this one, you need to increment the row everytime you want to create a new label

def print_names():
    row = 3
    for x in jnames:
        Label(gui, text=x[0]).grid(column=0, row=row)
        row += 1

def print_ages():
    row = 3
    for x in jnames:
        Label(gui, text=x[1]).grid(column=1, row=row)
        row += 1

def print_gender():
    row = 3
    for x in jnames:
        Label(gui, text=x[2]).grid(column=2, row=row)
        row += 1