I'm having trouble moving my functions into a separate file as they access a wxpython gui. The functions are from "onSaveMovieFile" to "LogThis"
#!/usr/bin/env python
# -*- coding: us-ascii -*-
# generated by wxGlade 0.6.3 on Fri Jul 22 11:53:07 2011
"""
Copyright (c) 2011 Mitchell Lafferty <coolspeedy6 at gmail dot com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR 开发者_开发百科A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
self.Bind(wx.EVT_CLOSE,self.OnClose)
self.Stop_Never.Bind(wx.EVT_TOGGLEBUTTON, self.onToggleNever)
self.On.Bind(wx.EVT_TOGGLEBUTTON, self.OnToggleOn)
self.Compile_every_now.Bind(wx.EVT_BUTTON, self.Compile)
self.Stills_Select.Bind(wx.EVT_BUTTON, self.onDirStills)
self.Movie_Select.Bind(wx.EVT_BUTTON, self.onSaveMovieFile)
self.Cap_every_SpinCtrl.Bind(wx.EVT_SPINCTRL,self.OnSpinUnlimited)
self.Comp_every_SpinCtrl.Bind(wx.EVT_SPINCTRL, self.OnSpinUnlimited)
self.Framerate_SpinCtrl.Bind(wx.EVT_SPINCTRL, self.OnSpinUnlimited)
self.Comp_Frames_SpinCtrl.Bind(wx.EVT_SPINCTRL, self.OnSpinUnlimited)
self.InitSizeAndQuality()
def onSaveMovieFile(self, event=None):
dlg = wx.FileDialog(
self, message="Save file as ...",
defaultFile="Timelapse.avi",
wildcard="Video files (*.avi)|*.avi",
style=wx.SAVE
)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
if not path.endswith(".avi"): path += ".avi" # NOTE: Not safe?
self.Movie_Input.SetValue(path)
dlg.Destroy()
def onDirStills(self, event=None):
dlg = wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE
## | wx.DD_DIR_MUST_EXIST
## | wx.DD_CHANGE_DIR
)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
if not path.endswith("/"): path += "/" # NOTE: Not safe?
self.Stills_Input.SetValue(path)
dlg.Destroy()
def onToggleNever(self,event=None):
self.Stop_Date.Enable(not self.Stop_Never.GetValue())
def OnToggleOn(self,event=None):
self.Snap()
self.Compile()
def Snap(self,event=None):
"""
NOTE: returns cause late work,
if "self.CheckDueDate()" is at bottom
this makes it execed deadline also,
late work should be a user option.
"""
# Haha, "Snapshot and save" sounds like a photo discount store!
print "Snapshot and save code here..."
if not self.Latework.GetValue(): self.CheckDueDate() # NOTE: does not cause late work
try:
if self.On.GetValue():
CapUnit = self.Cap_every_append.GetSelection()
CapVal = self.Cap_every_SpinCtrl.GetValue()
CapVal = self.Time2seconds(CapVal,CapUnit)
print CapVal
if CapVal > 0:
CapTimer = threading.Timer(CapVal,self.Snap)
CapTimer.start()
except Exception as err:
print "Error:", err
self.On.SetValue(False)
if not self.On.GetValue():
if 'CapTimer' in vars():
CapTimer.cancel()
return
path = self.Stills_Input.GetValue()
file = ""
if not os.path.exists(path):
print "Error opening: " + path
return
try:
capture = cv.CreateCameraCapture(-1)
if not capture:
print "Error opening: Camera device"
return
#cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 640 );
#cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 480 );
frame = cv.QueryFrame(capture)
if frame is None: return
file = time.strftime("%Y%m%d%H%M%S", time.gmtime())
file = path + "TS" + file + ".png"
cv.SaveImage(file, frame)
except Exception as err:
print "Error getting/saving frame: ", err
if os.path.isfile(file):
try:
self.Picture.SetBitmap(wx.Bitmap(file, wx.BITMAP_TYPE_ANY))
except Exception as err:
print "Can't show picture!: ", err
else:
print "Error opening: " + file
return
print file
if self.Latework.GetValue(): self.CheckDueDate() # NOTE: causes late work
##if 'CapTimer' in vars(): CapTimer.start()
def Compile(self,event=None):
print "Compile into movie code here.."
if not self.Latework.GetValue(): self.CheckDueDate() # NOTE: does not cause late work
try:
if self.On.GetValue():
CompUnit = self.Comp_every_append.GetSelection()
CompVal = self.Comp_every_SpinCtrl.GetValue()
CompVal = self.Time2seconds(CompVal,CompUnit)
print CompVal
if CompVal > 0:
CompTimer = threading.Timer(CompVal,self.Compile)
CompTimer.start()
except Exception as err:
print "Error:", err
self.On.SetValue(False)
if not self.On.GetValue():
if 'CompTimer' in vars():
CompTimer.cancel()
return
file = self.Movie_Input.GetValue()
path = self.Stills_Input.GetValue()
TimeApart = self.Time2seconds( self.Comp_Frames_SpinCtrl.GetValue(), self.Comp_Frames_append.GetSelection())
if not os.path.exists(path):
self.LogThis("opening: " + path,None,False)
return
try:
fps = self.Framerate_SpinCtrl.GetValue()
if fps<=0: fps = 10
frames = glob.glob(path + "TS*.png")
writer = cvCreateVideoWriter(file, -1, fps, is_color=1) # frame_size=-1
for i in range(len(frames)):
if TimeApart > 0:
frames[i] = re.search('TS(.*)\.png', frames[i]) # find correct file
frames[i] = frames[i].group(0) # grab date
frames[i] = mktime(time.strptime(frames[i], "%Y%m%d%H%M%S")) # make into timestamp
frames[i] += TimeApart # add to timestamp
frames[i] = path + time.strftime("TS%Y%m%d%H%M%S.png", time.gmtime(frames[i])) # make path+file place
if os.path.isfile(frames[i]):
self.ImageResize(frames[i])
cvWriteFrame(writer, frames[i])
except Exception as err:
self.LogThis("Can't make movie: " + str(err),None,False)
if self.Latework.GetValue(): self.CheckDueDate() # NOTE: causes late work
##if 'CompTimer' in vars(): CompTimer.start()
def CheckDueDate(self):
if self.Stop_Never.GetValue(): return
print "Timer duedate code here..."
selected = self.Stop_Date.GetValue()
month = selected.Month + 1
day = selected.Day
year = selected.Year
date_str = "%4d%02d%02d" % (year, month, day)
#?print date_str
#?print time.strftime("%Y%m%d", time.gmtime())
if date_str <= time.strftime("%Y%m%d", time.gmtime()):
print "Deadline reached!"
self.On.SetValue(False)
if 'CapTimer' in vars(): CapTimer.cancel()
if 'CompTimer' in vars(): CompTimer.cancel()
##self.OnToggleOn()
def Time2seconds(self,time,unit):
# errors "long int too large to convert to c long"
# unit == (anything else) # seconds 1
if unit == 1:
time *= 60 # minutes 60
elif unit == 2:
time *= 60 * 60 # hours 60 * 60
elif unit == 3:
time *= 24 * 60 * 60 # days 24 * 60 * 60
elif unit == 4:
time *= 365 * 24 * 60 * 60 # years 365 * 24 * 60 * 60
return time
def OnSpinUnlimited(self,event=None):
obj = event.GetEventObject()
obj.SetRange(0, obj.GetValue() + 100)
def OnClose(self,event=None):
if 'CapTimer' in vars(): CapTimer.cancel()
if 'CompTimer' in vars(): CompTimer.cancel()
self.Destroy()
def ImageResize(self,file):
"""##
try:
from PIL import Image
except Exception as err:
print 'We need This module!: ', err
self.Size_Combo.Enable(False)
self.Quality_SpinCtrl.Enable(False)
self.label_7.Enable(False)
return
##"""
try:
OldImage = Image.open(file)
# pil_image.size pil_image.format pil_image.mode
SizeVal = self.Size_Combo.GetValue()
Size = SizeVal.split('x', 1);
if Size[0] <= 0 or Size[1] <= 0: return
NewImage = OldImage.resize((Size[0], Size[1]), Image.ANTIALIAS)
NewImage.save(file,quality=Quality_SpinCtrl.GetValue())
except Exception as err:
self.Error_GUI_Print_Log('resize: ' + str(err),None,False)
def InitSizeAndQuality(self):
try:
from PIL import Image
except Exception as err:
self.LogThis('We need This module!: ' + str(err))
self.Size_Combo.Enable(False)
self.Quality_SpinCtrl.Enable(False)
self.Size_and_Quality_label.Enable(False)
def LogThis(self,msg,level=None,LOUD=True):
if level == None: level = logging.ERROR
LEVELS ={}
LEVELS[logging.DEBUG]=_('Debug')
LEVELS[logging.INFO]=_('Info')
LEVELS[logging.WARNING]=_('Warning')
LEVELS[logging.ERROR]=_('Error')
LEVELS[logging.CRITICAL]=_('Critical')
print LEVELS[level],":",msg
if LOUD:
try:
wx.MessageBox(msg,LEVELS[level])
except:
pass
if NoLog: return #'NoLog' in vars() and
logger = logging.getLogger('YaTLC')
hdlr = logging.FileHandler('./YaTLC.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
#logger.setLevel(logging.INFO)
logger.log(level,msg)
#logger.shutdown()
PS: I fixed it, all I did was put the function in a different file, imported it, and replaced some "self's" with the name of the different file
I see you are using wxglade for the design of you gui. The best procedure is to keep the code from glade untouched in a file. Then you import and subclass the wxglade created class in a new file. There you write all your Bind() and functions there.
For example suposse this is your wxglade autogenerated file:
mygui.py
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
# generated by wxGlade HG on Tue Jul 26 20:03:16 2011
import wx
# begin wxGlade: extracode
# end wxGlade
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.text_ctrl_1 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE)
self.__set_properties()
self.__do_layout()
# end wxGlade
def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle("frame_1")
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.text_ctrl_1, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
# end wxGlade
# end of class MyFrame
Then in your application:
myaplication.py
import wx
from mygui import MyFrame
class MyApplication(MyFrame):
def __init__(self, *args, **kargs):
MyFrame.__init__(self, *args, **kargs)
##**put here all your Bind()**
#**then put all your methods here:**
def onSaveMovieFile(self, evt):
----------------
def LogThis(self, evt):
----------------
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyApplication(None)
frame.Show()
app.MainLoop()
精彩评论