I have the code below :
def update_contact_from_crm_to_import_in_365():
sql = "SELECT * FROM CRM_PRIMARY WHERE copy_to_365 = 'To update' ORDER BY CRM_PRIMARY.id DESC"
search_result = my_cursor.execute(sql)
search_result = my_cursor.fetchall()
sql_result.clear()
sql_result.append(search_result)
Filter(sql_result[0])
# Get id of sql result to pass them
list_get_selected_id.clear()
for res in (sql_result):
for id in (res):
list_get_selected_id.append(id[0])
time.sleep(5)
validate1 = messagebox.askquestion("Update ?", "Launch update ?")
if validate1 == 'yes':
open_dynamics_365_full_contact_list()
My problem is that the Filter(sql_result[0])
seems to start at the very end of the update_contact_from_crm_to_import_in_365
function (after the messagebox
)
So far, when i call update_contact_from_crm_to_import_in_365()
it display the messagebox and once it has been answered then it call Filter()
.
But i would like to call Filter
, wait 5 secondes for Filter to proceed and end, and then display the messagebox.
How do i do that please ?
I read about Threading but i'm just a beginner in Python so it's a bit complicated for me to get it yet.
Thanks in advance
I can't run it but problem can be that mainloop
first runs function update_contact_from_crm_to_import_in_365
and it waits for it end and later it updates widgets in widow. This way it can update all widgets in one moment so window less flicker.
In your situation you can have two solutions
You can use root.after(5000, function_with_message)
instead of sleep()
and then update_contact_from_crm_to_import_in_365
will finish work and it will go back to mainloop
which will update widgets - and after 5000ms it will run function_with_message
.
def show_message():
validate1 = messagebox.askquestion("Update ?", "Launch update ?")
if validate1 == 'yes':
open_dynamics_365_full_contact_list()
def update_contact_from_crm_to_import_in_365():
sql = "SELECT * FROM CRM_PRIMARY WHERE copy_to_365 = 'To update' ORDER BY CRM_PRIMARY.id DESC"
search_result = my_cursor.execute(sql)
search_result = my_cursor.fetchall()
sql_result.clear()
sql_result.append(search_result)
Filter(sql_result[0])
# Get id of sql result to pass them
list_get_selected_id.clear()
for res in (sql_result):
for id in (res):
list_get_selected_id.append(id[0])
root.after(5000, show_message)
#root.after(1, show_message) # it will no need to wait so long.
You can use root.update()
after Filter()
to force mainloop
to update widgets before end of function.
def update_contact_from_crm_to_import_in_365():
sql = "SELECT * FROM CRM_PRIMARY WHERE copy_to_365 = 'To update' ORDER BY CRM_PRIMARY.id DESC"
search_result = my_cursor.execute(sql)
search_result = my_cursor.fetchall()
sql_result.clear()
sql_result.append(search_result)
Filter(sql_result[0])
# Get id of sql result to pass them
list_get_selected_id.clear()
for res in (sql_result):
for id in (res):
list_get_selected_id.append(id[0])
root.update() # force tkinter to update widgets in window
#time.sleep(5)
validate1 = messagebox.askquestion("Update ?", "Launch update ?")
if validate1 == 'yes':
open_dynamics_365_full_contact_list()
In both situations you can use smaller values because it will run it after updating widgets so it doesn't need time for this.