0

I've used 'for' loops to create tables made from 'Label' objects. Apart from the 'divider', each loop appends to it's own two-dimensional array(each column has thirteen 'Labels'); I wanted to give every object an index position within their respective tables, so that I can then reference those points and alter the contents of the 'Label'.

#--------------------------------
    entry_grid_exp=[]
    entry_grid_req=[]
#--------------------------------
    exp_list = Frame(program, bg="#dcdcdc", height=500, width=600)
    exp_list.grid(row=3, column=1)
#--------------------------------
    for i in range(1, 16):
        entry_column=[]
        for x in range(0, 13):
            entry_column.append(Label(exp_list, width=15,bg="#FFFFFF",relief=SUNKEN))
            entry_column[x].grid(row = x+6, column=i, padx=0, pady=0)
        entry_grid_exp.append(entry_column)
    Request_list = Label(exp_list, width=10, bg="#dcdcdc", fg="#000000", text="Requested list.")
    Request_list.grid(row = 20, column=1)
    for i in range(1,16):
        box3 = Label(exp_list, width=10, bg="#dcdcdc", fg="#dcdcdc", text="divider")
    box3.grid(row = 21, column=i)
    for i in range(1, 16):
        entry_column=[]
        for x in range(0, 6):
            entry_column.append(Label(exp_list, width=15,bg="#FFFFFF",relief=SUNKEN))
            entry_column[x].grid(row = x+21, column=i, padx=0, pady=0)
         entry_grid_req.append(entry_column)
#--------------------------------

A submit button calls the function 'stock_append' and passes on the 'entry_grid_exp' and 'entry_grid_req' arguments. I read a post which said that placing the list into a 'for' loop and attributing every item within the list to 'Label' will allow me to call the 'configure' function, but it doesn't seem to work. Additionally this loop (if it worked) would configure every item within 'entry_grid_exp' rather than one specific position.

def stock_append(entry_grid_exp, entry_grid_req):
for Label in entry_grid_exp:
    Label.configure(text="test") 

I'm trying to complete this project and am completely lost, and absolutely appreciate any help because I have no idea what I'm doing.

UPDATED EXAMPLE

from tkinter import *
root = Tk()
Med_font = ("Helvatica", 11)
Small_font = ("Helvatica", 9)
main = Frame(root, bg="#dcdcdc", height=600, width=600)
main.grid()
s_name = StringVar()
s_category = StringVar()
s_description = StringVar()
s_quantity = StringVar()
s_risk = StringVar()
s_ID = StringVar()


def stock_append(entry_grid_exp, entry_grid_req, program):
    for item in entry_grid_exp:
        Label.configure(program, text="test")


def stock_define(entry_grid_exp, entry_grid_req, program):
    stock_frame = Frame(main, bg="#dcdcdc", height=100, width=1000)
    stock_frame.grid()

    name = Entry(stock_frame, width=15, textvariable=s_name)
    name.grid(row=1, column=1, padx=10)

    category = Entry(stock_frame, width=15, textvariable=s_category)
    category.grid(row=1, column=2, padx=10)

    description = Entry(stock_frame, width=15, textvariable=s_description)
    description.grid(row=1, column=3, padx=10)

    quantity = Entry(stock_frame, width=15, textvariable=s_quantity)
    quantity.grid(row=1, column=4, padx=10)

    risk = Entry(stock_frame, width=15, textvariable=s_risk)
    risk.grid(row=1, column=5, padx=10)

    stock_id = Entry(stock_frame, width=15, textvariable=s_ID)
    stock_id.grid(row=1, column=6, padx=10)
    submit = Button(stock_frame, width=15, text="submit", command=lambda: stock_append(entry_grid_exp, entry_grid_req,
                                                                                  program))
    submit.grid(row=1, column=7, padx=10)


def stock_lists():
    program = Frame(main, bg="#dcdcdc", height=600, width=600)
    program.grid()

    title = Label(program, text="MAIN", font=Med_font, bg="#dcdcdc")
    title.grid(row=2, column=1)

    entry_grid_exp = []
    entry_grid_req = []

    exp_list = Frame(program, bg="#dcdcdc", height=500, width=600)
    exp_list.grid(row=3, column=1)

    stock_add = Button(exp_list, bg="#bebebe", width=10, text="ADDSTCK", command=lambda: stock_define(entry_grid_exp,
                                                                                                  entry_grid_req,
                                                                                                  program))
    stock_add.grid(row=4, column=15)

    experiment_list = Label(exp_list, width=10, bg="#dcdcdc", fg="#000000", text="Experiment list.")
    experiment_list.grid(row=4, column=1)

    for i in range(1, 16):
        entry_column = []
        for x in range(0, 13):
            entry_column.append(Label(exp_list, width=15, bg="#FFFFFF", relief=SUNKEN))
            entry_column[x].grid(row=x+6, column=i, padx=0, pady=0)
        entry_grid_exp.append(entry_column)

    for i in range(1, 16):
        divider = Label(exp_list, width=10, bg="#dcdcdc", fg="#dcdcdc", text="divider")
        divider.grid(row=21, column=i)

    request_list = Label(exp_list, width=10, bg="#dcdcdc", fg="#000000", text="Requested list.")
    request_list.grid(row=20, column=1)

    for i in range(1, 16):
        entry_column = []
        for x in range(0, 6):
            entry_column.append(Label(exp_list, width=15, bg="#FFFFFF", relief=SUNKEN))
            entry_column[x].grid(row=x+21, column=i, padx=0, pady=0)
        entry_grid_req.append(entry_column)

stock_lists()
mainloop()
SebG
  • 9
  • 4
  • How are you assigning your command to your button? I mean, `Button(....., command=_whatIsWritingHere_)`. I am asking because there is common mistake about it which is answered here. http://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared – Lafexlos Dec 02 '16 at 13:06
  • 4
    What does "doesn't seem to work" mean? Do you get errors? If so, what errors? If not, what happens, and how is that different from what you expect? – Bryan Oakley Dec 02 '16 at 13:06
  • 1
    FWIW, your code has a couple of lines that look like they're incorrectly indented. However, it's difficult for us to test your code (or modify it) because it's not a [mcve]. BTW, using `Label` as a variable name in `for Label in entry_grid_exp:` is a bad idea since that clashes with Tkinter's use of that name. Of course, you wouldn't have that problem if you didn't do `from tkinter import *`... – PM 2Ring Dec 02 '16 at 13:18
  • Thank you @Lafexlos for your comment. I have edited my post to conform with PEP8 guide lines and have produced a Minimal, Complete, and Verifiable example. – SebG Dec 04 '16 at 20:11
  • Thank you @BryanOakley for your comment. I have edited my post to conform with PEP8 guide lines and have produced a Minimal, Complete, and Verifiable example. – SebG Dec 04 '16 at 20:12
  • Thank you @PM2Ring for your comment. I have edited my post to conform with PEP8 guide lines and have produced a Minimal, Complete, and Verifiable example. – SebG Dec 04 '16 at 20:12

1 Answers1

0

Your updated example is better, but it's certainly not minimal. We don't need that large grid of Labels to illustrate your problem. (I have an old monitor so my desktop is only 1024 x 768 pixels, which makes it painful to view your GUI). Also, it's still not totally clear what you want the program to do. But anyway...

Your entry_grid_exp, entry_grid_req are both lists of lists, with the inner lists being columns of Labels, so to access those Labels you need to use an appropriate loop structure.

Here's a version of stock_append that gives each Label a text based on its row and column number.

def stock_append(entry_grid_exp, entry_grid_req, program):
    x = 0
    for col in entry_grid_exp:
        y = 0
        for item in col:
            item.configure(text="exp{}_{}".format(y, x))
            y += 1
        x += 1

    x = 0
    for col in entry_grid_req:
        y = 0
        for item in col:
            item.configure(text="req{}_{}".format(y, x))
            y += 1
        x += 1

Here's a more compact version that uses the built-in enumerate function to produce the row and column numbers:

def stock_append(entry_grid_exp, entry_grid_req, program):
    for x, col in enumerate(entry_grid_exp):
        for y, item in enumerate(col):
            item.configure(text="exp{}_{}".format(y, x))

    for x, col in enumerate(entry_grid_req):
        for y, item in enumerate(col):
            item.configure(text="req{}_{}".format(y, x))

The entry_grid_exp and entry_grid_req lists are local to the stock_lists function. If you want them to exist after stock_lists finishes running you need to return them to the code that calls stock_lists.

BTW, the name of the font is "Helvetica", not "Helvatica".

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182