Seite 1 von 1

Threading problem

Verfasst: Dienstag 18. April 2006, 23:38
von Alexci
Hallo,

Ich habe ein problem mit threading.

Code: Alles auswählen

from Tkinter import *
from PIL import Image, ImageTk 
import win32com.client
import math
import time
import os
import threading

Document = win32com.client.Dispatch('MaxIm.Document')
FM = win32com.client.Dispatch('FocusMax.FocusControl')
CCDCamera = win32com.client.Dispatch('MaxIm.CCDCamera')
Application = win32com.client.dynamic.Dispatch('MaxIm.Application') 

class TestThread (threading.Thread):
  def __init__(self):
     threading.Thread.__init__(self, name='TestThread')
  def run (self):

      path_to_watch = "F:/Images/VRT/"  
      before = dict ([(f, None) for f in os.listdir (path_to_watch)])
      while 1:
        time.sleep(2)
        after2 = dict ([(f, None) for f in os.listdir (path_to_watch)])
        added = [f for f in after2 if not f in before]
        if added:
                name= ' ,'.join (added)
                if str(name[-3:])=='fit':
                            print name
                            Document.OpenFile('F:/Images/VRT/'+name) 
                            Document.SaveFile('F:/Images/VRT/'+ str(name[0:-4])+'.jpg', 6, 1024,2)
                            Application.CloseAll()
                before = after2

TestThread().start()

Die Error Nachricht sieht so aus:

Exception in thread TestThread:
Traceback (most recent call last):
File "C:\Program Files\Python\lib\threading.py", line 442, in __bootstrap
self.run()
File "G:\Robot teleskop\VRT\soft\GUI.py", line 44, in run
Document.OpenFile("F:/Images/VRT/"+name)
File "C:\Program Files\Python\Lib\site-packages\win32com\client\dynamic.py", line 496, in __getattr__
raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: MaxIm.Document.OpenFile


Was ist das fuer ein Error: "raise AttributeError, "%s.%s" % (self._username_, attr)"?

Sonst funktioniert es (Document.Open() und Document.Save()), wenn ich es nicht im Thread starte und wenn "def run (slef)", nicht im code ist, aber dann funktioniert der rest des Programms nicht.
Weiss jehmand wo der Problem liegt? Warum funktioniert es nicht nur, wenn "def run (slef)" im Code ist?


Danke in Vorraus!

Mit freundlichen Gruessen,
Aleksandar


P.S. Habe auch gegooglet. Vielleicht kan euch das helfen:
http://www.gossamer-threads.com/lists/p ... hon/105254

Wenn ja, koennte mir jehmand erklaehren wie man das macht: "Get the module either via cache (ID of the winfay library , Locale, VersionMajor, VersionMinor)", ich bin Anfaenger in Python...

Re: Threading problem

Verfasst: Mittwoch 19. April 2006, 08:42
von gerold
Alexci hat geschrieben:Hallo,

Ich habe ein problem mit threading.

Sonst funktioniert es (Document.Open() und Document.Save()), wenn ich es nicht im Thread starte und wenn "def run (slef)", nicht im code ist,
Hi!

Vielleicht helfen diese Links weiter:
http://www.python-forum.de/post-33008.html#33008
http://www.python-forum.de/post-32993.html#32993

Ich würde zumindest mal versuchen, die COM-Schnittstellen erst innerhalb der run-Methode zu definieren.

mfg
Gerold
:-)

Threating problem geloest

Verfasst: Mittwoch 19. April 2006, 09:24
von Alexci
Vielen Dank fuer Ihr Tipp. Es hat mir sehr geholfen. Jetzt funktioniert es!!!

Hier nochmahl der richtige Code fuer die Archive:

Code: Alles auswählen

class TestThread (threading.Thread):
  def __init__(self):
     threading.Thread.__init__(self, name='TestThread')
  def run (self):
      pythoncom.CoInitialize()
      try:
          Document = win32com.client.Dispatch('MaxIm.Document')
          FM = win32com.client.Dispatch('FocusMax.FocusControl')
          CCDCamera = win32com.client.Dispatch('MaxIm.CCDCamera')
          Application = win32com.client.dynamic.Dispatch('MaxIm.Application')

          path_to_watch = "F:/Images/VRT/"  
          before = dict ([(f, None) for f in os.listdir (path_to_watch)])
          while 1:
            time.sleep(2)
            after2 = dict ([(f, None) for f in os.listdir (path_to_watch)])
            added = [f for f in after2 if not f in before]
            if added:
                    name= ' ,'.join (added)
                    if str(name[-3:])=='fit':
                                print name
                                Document.OpenFile('F:/Images/VRT/'+name) 
                                Document.SaveFile('F:/Images/VRT/'+ str(name[0:-4])+'.jpg', 6, 1024,2)
                                Application.CloseAll()
                    before = after2
      finally: 
          try: 
              pythoncom.CoUninitialize() 
          except: 
              pass 


TestThread().start()
Mit freundlichen Gruessen,
Aleksandar Cikota

Open.Image in Thread

Verfasst: Mittwoch 19. April 2006, 15:12
von Alexci
Hi,

Ich haette hier noch ein kleines Problem. Wenn ich ein Bild oeffnen will, bekomme ich diese Error Meldung:

Exception in thread TestThread:
Traceback (most recent call last):
File "C:\Program Files\Python\lib\threading.py", line 442, in __bootstrap
self.run()
File "G:\Robot teleskop\VRT\soft\GUI.py", line 60, in run
image = Image.open('G:/Robot teleskop/VRT/'+ str(name[0:-4])+'.jpg')
AttributeError: 'function' object has no attribute 'open'



Hier nochmahl der ganze Code:

Code: Alles auswählen

from Tkinter import *
from PIL import Image, ImageTk
from win32com.client import gencache 
import tkMessageBox
import win32com.client
import math
import time
import os
import threading
import pythoncom 

chsr = win32com.client.Dispatch("DriverHelper.Chooser") 
chsr.DeviceType = "Telescope"
scopeProgID = chsr.Choose("scopeProgID")
Scope = win32com.client.Dispatch(scopeProgID)

#Scope = win32com.client.dynamic.Dispatch('ScopeSim.Telescope')

Document = win32com.client.Dispatch('MaxIm.Document')
FM = win32com.client.Dispatch('FocusMax.FocusControl')
CCDCamera = win32com.client.Dispatch('MaxIm.CCDCamera')
Application = win32com.client.dynamic.Dispatch('MaxIm.Application') 

Scope.SiteElevation = 230
Scope.SiteLatitude = 45.2777
Scope.SiteLongitude = 13.7261




root = Tk()

class TestThread (threading.Thread):
  def __init__(self):
     threading.Thread.__init__(self, name='TestThread')
  def run (self):
      pythoncom.CoInitialize()
      try:

          Document = win32com.client.Dispatch('MaxIm.Document')
          FM = win32com.client.Dispatch('FocusMax.FocusControl')
          CCDCamera = win32com.client.Dispatch('MaxIm.CCDCamera')
          Application = win32com.client.dynamic.Dispatch('MaxIm.Application')

          path_to_watch = "F:/Images/VRT/"  
          before = dict ([(f, None) for f in os.listdir (path_to_watch)])
          while 1:
            time.sleep(2)
            after2 = dict ([(f, None) for f in os.listdir (path_to_watch)])
            added = [f for f in after2 if not f in before]
            if added:
                    name= ' ,'.join (added)
                    if str(name[-3:])=='fit':
                                print name
                                Document.OpenFile('F:/Images/VRT/'+name) 
                                Document.SaveFile('F:/Images/VRT/'+ str(name[0:-4])+'.jpg', 6, 512,2)
                                Document.SaveFile('F:/Images/VRT/lastimage.jpg', 6, 512,2)
                                Application.CloseAll()

                                image = Image.open('G:/Robot teleskop/VRT/'+ str(name[0:-4])+'.jpg') 
                                photo = ImageTk.PhotoImage(image) 
                                Label(image=photo).grid(row=0, column=10, columnspan=15, rowspan=29, sticky=W+E+N+S, padx=5, pady=5)

                    before = after2
      finally: 
          try: 
              pythoncom.CoUninitialize() 
          except: 
              pass 


TestThread().start()


#[CUT]


root.mainloop()

Etwas fehlt hier im Code, ich weiss aber nicht was.


Mit freundlicehn Gruessen,
Aleksandar Cikota

Re: Open.Image in Thread

Verfasst: Mittwoch 19. April 2006, 17:50
von gerold
Alexci hat geschrieben:

Code: Alles auswählen

                                image = Image.open('G:/Robot teleskop/VRT/'+ str(name[0:-4])+'.jpg') 
                                photo = ImageTk.PhotoImage(image)
Hi Aleksandar!

So könnte es funktionieren:

Code: Alles auswählen

image = Image()
image.open('G:/Robot teleskop/VRT/'+ str(name[0:-4])+'.jpg')
mfg
Gerold
:-)

Verfasst: Mittwoch 19. April 2006, 19:45
von Alexci
Leider funktioniert es nicht. :(

Die Error Meldung sieht so aus:
Exception in thread TestThread:
Traceback (most recent call last):
File "C:\Program Files\Python\lib\threading.py", line 442, in __bootstrap
self.run()
File "G:\Robot teleskop\VRT\soft\GUI.py", line 44, in run
image = Image()
TypeError: 'module' object is not callable



Gruss,
Aleksandar

Verfasst: Mittwoch 19. April 2006, 20:00
von gerold
Alexci hat geschrieben:TypeError: 'module' object is not callable
Hi Aleksandar!

Das habe ich übersehen: Du musst wahrscheinlich "Image" anders importieren.
Siehe: http://www.pythonware.com/library/pil/h ... /image.htm

mfg
Gerold
:-)

Problem geloest

Verfasst: Mittwoch 19. April 2006, 23:58
von Alexci
Hallo,

Ich weiss wo der Fehler war. Der Path war falsch definiert :D :oops:

Trotzdem, Danke!

Gruss,
Aleksandar