Python スレッドのエラー Exception in thread Thread
Q&A
import datetime , os
import tkinter as tk
import threading
import win32com.client
def counter():
class Timer:
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(self.root)
self.label["font"] = ("Helvetica", 25)
self.label["bg"] = "blue"
self.label["fg"] = "white"
self.label.grid(column=0, row=0)
self.root.title(u'処理時間まで')
def changeLabelText(self):
while True:
acttime = datetime.datetime.combine(datetime.date.today(), datetime.time(13,18,0))
nowtime = datetime.datetime.now()
ato = acttime - nowtime
atoh = int(ato.seconds / 3600)
atom = int((ato.seconds / 3600 - int(ato.seconds / 3600)) * 60)
ctntxt = '処理時間まであと' + str(atoh) + '時間' + str(atom) + '分'
self.label["text"] = ctntxt
if atoh + atom == 0:
Excel = win32com.client.Dispatch('Excel.Application')
Excel.Visible = True
Excel.DisplayAlerts = False
filename = '照合.xlsm'
fullpath = os.path.join(os.getcwd(), filename)
wb = Excel.Workbooks.Open(Filename=fullpath)
wb.Close(SaveChanges=False)
if __name__ == "__main__":
timer = Timer()
thread1 = threading.Thread(target=timer.changeLabelText)
thread1.start()
timer.root.mainloop()
counter()
上記のコードを実行したところ・・・
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\USER\anaconda3\envs\PJ1\lib\site-packages\win32com\client\dynamic.py", line 81, in _GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221008, 'CoInitialize は呼び出されていません。', None, None)
というエラーが出て処理が進められません。
原因・回避方法をご教授いただきますよう何卒よろしくお願いいたします。
0 likes