将 python 代码移动到单独的文件
当我的函数访问 wxpython gui 时,我无法将它们移动到单独的文件中。 这些函数是从“onSaveMovieFile”到“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:我修复了它,我所做的只是将函数放在不同的文件中,导入它,并用不同文件的名称替换一些“self”
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到你正在使用 wxglade 来设计你的 gui。
最好的过程是将来自空地的代码保持在文件中不变。然后在新文件中导入 wxglade 创建的类并对其进行子类化。在那里你可以编写所有的 Bind() 和函数。
例如,假设这是您的 wxglade 自动生成的文件:
mygui.py
然后在您的应用程序中:
myaplication.py
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
Then in your application:
myaplication.py