Trying to correlate functions in Tkinter

  for-loop, function, loops, python, tkinter

I have this GUI in Tkinter where you can press a a button and after that a toplevel appears where you can input data in different entries. That data is saved in an Excel file. I am stuck right now when im trying to exit from place n° 1 (after pressing button 1 and top.withdraw from that toplevel) and going to place n° 2 (by pressing button 2). What i want is that the for loop i made for the entries re-start in place n° 2. Instead, what is actually doing is continuing the for loop in the same stop it was in place n° 1. Is continuing the for loop, and i want to break it somehow to start over again in place n° 2. Since this code has 500+ lines i will paste here the proper part of the code and specifically the part i thing must me modified is the last part of the code. Thank you all !

def crear_grilla(comando):
top = Toplevel(root)
top.geometry("1150x600")
height_value=top.winfo_screenheight()
width_value=top.winfo_screenwidth()
top.geometry("%dx%d+0+0" % (width_value, height_value))
frame1 = Frame(top,height=5,width=5)
frame1.grid(row=6)
for y in range (10):
    #for x in range(2):
        my_label = Label(top,text="CUIJ",font = "Arial 24 bold")
        my_label.grid(row=0,column=0,sticky="N")
        my_label1 = Label(top,text="Observación",font = "Arial 24 bold")
        my_label1.grid(row=0,column=1,sticky="N")
        my_label2 = Label(top, width=5,text="21-0",font = "Arial 12").grid(padx=5, pady=5,sticky = 'w', row=y+1, column=0)
        my_label3 = Label(top, width=5,text="Observ",font = "Arial 12").grid(padx=5, pady=5,sticky = 'w', row=y+1, column=1)
        
        my_entry = Entry(top,width=40,font = "Arial 12 bold")
        my_entry.grid(row=y+1,column=0,padx=65,pady=15,sticky = "e",ipadx=20,ipady=2)
        my_entry.focus() 
        my_entries.append(my_entry)
    
        my_entry1 = Entry(top,width=40,font = "Arial 12 bold")
        my_entry1.grid(row=y+1,column=1,padx=65,pady=15,sticky = "e",ipadx=20,ipady=2)
        my_entries1.append(my_entry1)

my_button2 = Button(top, text="Continuar",command=comando,font = "Arial 13 bold")
my_button2.grid(row=20,column=1, sticky = 'w',padx=30)

def destruir_ventana():
    top.withdraw()
    for i in range(2,12):
        hoja.cell(row=i, column= 1, value="")
        hoja.cell(row=i, column= 2 , value="")
        fila += 1
    book.save('C:Programa_paseslista_CUIJ.xlsx')

my_button3 = Button(top, text="SALIR",command=destruir_ventana,font = "Arial 13 bold")
my_button3.grid(row=20,column=2, sticky = '')


def guardar_datos():
    columnas = 1
    fila = 2
    valor = "21-0"

    for x in range(0, len(my_entries)):
        if not my_entries[x].get().isspace() and not my_entries1[x].get().isspace():
            hoja.cell(row=fila, column= columnas, value=valor+my_entries[x].get()).font = Font(name='Arial', size=13)
            hoja.cell(row=fila, column= columnas+1 , value=my_entries1[x].get()).font = Font(name='Arial', size=13)
        if len(my_entries[x].get()) <= 4 or my_entries[x].get().isalpha() == True:
            hoja.delete_rows(fila)
            fila -=1
        fila += 1
    book.save('C:Programa_paseslista_CUIJ.xlsx')

my_button1 = Button(top, text="Guardar",command=guardar_datos,font = "Arial 13 bold")
my_button1.grid(row=20,column=0, sticky = 'e',padx=30)

Source: Python Questions

LEAVE A COMMENT