0

I have created one python tkinter app using selenium and xlwings modules. complete selenium and xlwings code are inside a function and that function I am calling through a thread and getting this error: (-2147417842, 'The application called an interface that was marshalled for a different thread.', None, None) And if I don't use thread, it works fine, but Tkinter window does not respond when I try to exit. Please help me with both the solutions (If I use threading and if I don't use threading) I have tried other answers on StackOverflow but all of them were confusing.

from tkinter import *
import threading
import xlwings as xw

def start_skip():
    with xw.App(visible=False) as app:
        book = xw.Book(uploadEntry.get())
        sheet = book.sheets['Sheet1']
        address = sheet.range((excel_row, col)).value

        //some selenium code to fetch data and add to excel
    
        book.save()
        book.close()

def start_thread():
    t1 = threading.Thread(target=start_skip)
    t1.daemon=True
    t1.start()



root=Tk()
uploadEntry=Entry(root)
uploadEntry.pack()
button=Button(root,text='START',command=start_thread)
button.pack()
root.mainloop()
  • Can you please post the full error traceback? Also it isn't good practise to call `tkinter` methods from threads other than the one where you create your `tk.Tk()` – TheLizzard Oct 15 '22 at 18:22
  • first you could reduce code to see which element makes problem - maybe it has nothing to do with `selenium` and `Excel`. Tkinter doesn't like to work in threads and you could rather do `Thread(target=start_skip, args=(uploadEntry.get(),))` and `def start_skip(text):` and later `book = xw.Book(text)` – furas Oct 15 '22 at 20:03
  • You can also look at this post on using win32com with multithreading https://stackoverflow.com/questions/26764978/using-win32com-with-multithreading/27966218#27966218 – moken Oct 31 '22 at 08:39

0 Answers0