Search in list of tkinter widgets

Viewed 45

I have some tkinter widgets stored in a list. I'd like to search for an object in that list, by I don't know the syntax.

import tkinter as tk

main = tk.Tk()
base = tk.Frame(main).pack()

l = []

for i in range(3):
    et = tk.Label(base, text='label '+str(i))
    et.pack()
    l.append(et)

print(base.Label.!label in l)

main.mainloop()

Note. Certainly this is a minimal example to understand where my mistake is. The gui actually consists of an n x m matrix of tkinker entries, whose cells, rows, and columns should be dynamically added, deleted, modified, and even inserted or switched.

To do this, I have a dictionary that associates index tuples (i,j) with tkinter entries. When an entry is chosen with the mouse, that object is known, but what I really need to know is its index (i,j) to manage all the rest of the information (maths operations over arrays, etc).

enter image description here

1 Answers

To solve your problem you could use the index of separate list and use widget.grid_info() to retrieve the indices.

top_bar     = []
sidebar     = []

The rendering and the content have separated masters to achieve that the indexes of the lists, which starts with 0, match with the column and row of your inner grid. The outer master could be populated like this, important to note is that Label on grid position 0,0 is not append to any of the lists but is gridded to fill the space. The grid_configure is just a optical improvement.

def outer_matrix():
    for x in range(COLUMNS+1):
        for y in range(ROWS+1):
            ref = tk.Label(root,)#justify='center')
            txt = None
            if x == 0 and y != 0:
                txt = y
                top_bar.append(ref)
            elif y == 0 and x != 0:
                txt = x
                sidebar.append(ref)
            if txt != None or x == 0 and y == 0:
                ref.config(text = txt, width=10)
                ref.grid(row = y, column = x, sticky='nswe')
                root.grid_columnconfigure(x,weight=1)
                root.grid_rowconfigure(y,weight=1)

Full Example:

import tkinter as tk

ROWS = 6
COLUMNS = 6

top_bar     = []
sidebar     = []

def outer_matrix():
    for x in range(COLUMNS+1):
        for y in range(ROWS+1):
            ref = tk.Label(root,)#justify='center')
            txt = None
            if x == 0 and y != 0:
                txt = y
                top_bar.append(ref)
            elif y == 0 and x != 0:
                txt = x
                sidebar.append(ref)
            if txt != None or x == 0 and y == 0:
                ref.config(text = txt, width=10)
                ref.grid(row = y, column = x, sticky='nswe')
                root.grid_columnconfigure(x,weight=1)
                root.grid_rowconfigure(y,weight=1)
            else:
                ref.destroy()
                

def inner_matrix():
    for x in range(COLUMNS):
        for y in range(ROWS):
            ref = tk.Entry(inner_frame, width=10)
            ref.grid(column=x,row=y, sticky='nswe')
            inner_frame.grid_columnconfigure(x,weight=1)
            inner_frame.grid_rowconfigure(y,weight=1)
            ref.bind('<FocusIn>', lambda e:highlight(e))
            ref.bind('<FocusOut>', lambda e:highlight(e))


def highlight(event):
    info = event.widget.grid_info()
    x_info = info['row']
    y_info = info['column']
    if 'FocusIn' in str(event):
        top_bar[x_info].configure(bg='yellow')
        sidebar[y_info].configure(bg='yellow')
    if 'FocusOut' in str(event):
        top_bar[x_info].configure(bg='SystemButtonFace')
        sidebar[y_info].configure(bg='SystemButtonFace')
    
    


root = tk.Tk()
root.update()
inner_frame = tk.Frame(root,bg='red')
inner_frame.grid(row=1,column=1,
                 columnspan=COLUMNS,
                 rowspan = ROWS,
                 sticky = 'nswe')

outer_matrix()
inner_matrix()


root.mainloop()
Related