Home > database >  Simplifying this Tkinter For Loop to Update Entry Values between two columns
Simplifying this Tkinter For Loop to Update Entry Values between two columns

Time:11-29

This script works fine for doing what I want it to, but I think there's likely a far more direct alternative that I'm missing.

All I have is a tkinter checkbox that when pressed, runs this function that maps all the info from column 0's rows of entry boxes into column 1's entry boxes. Currently, this is the script:

def update_company_new():

    company = str()

    for child in frame.winfo_children():
        if child.grid_info()["column"] == 0:
            company = child.get()
        if child.grid_info()["column"] == 1:
            child.insert(0, company)

Is there a more direct way to do this? This seems something that could normally be done with a simple list comprehension but I can't find enough details on additional options for winfo_children() and grid_info() that make it adaptable to tkinter grid references.

CodePudding user response:

It can be done using list comprehension:

[frame.grid_slaves(row=w.grid_info()['row'], column=1)[0].insert(0, w.get()) for w in frame.grid_slaves(column=0)]

But it is hard to understand for beginner and it created an useless list of None.

It is better to use a simple for loop instead:

for w in frame.grid_slaves(column=0):
    row = w.grid_info['row']
    peer = frame.grid_slaves(row=row, column=1)
    # check if there is Entry in column 1
    if peer:
        peer[0].insert(0, w.get())
  • Related