WXPython错误处理和重复使用
我有一个wx python应用程序,当您单击按钮时,它会对特定输入执行一系列步骤。应用程序第一次工作,但是如果我第二次尝试用不同的输入值单击按钮,它就会抛出一个错误 我有两个问题: 1.如何修复下面的代码,使其停止抛出此错误消息 二,。我应该添加哪些特定代码,以便在对话框中向最终用户显示任何错误消息(如此消息)?我希望它能够优雅地终止从按钮单击开始抛出错误的整个过程,并用一个新的对话框替换进度条,该对话框给出错误消息,并允许用户单击一个按钮,该按钮将返回到应用程序的主窗口,以便他们可以尝试再次单击按钮使其成功使用新的输入。我很快就要将其封装到一个exe文件中,但目前唯一的错误处理是python shell中的错误消息。因为用户不会使用python shell,所以我需要按照本段中描述的方式显示错误消息。注意,这里我的OnClick函数实例化了一个类。我在实际代码中使用了许多不同的类,每个类都非常复杂,我需要这里要求的功能,以便能够处理由OnClick函数实例化的各种类的内部深处发生的错误 下面是我的代码的一个非常简化但有效的版本:WXPython错误处理和重复使用,python,error-handling,wxpython,progress-bar,Python,Error Handling,Wxpython,Progress Bar,我有一个wx python应用程序,当您单击按钮时,它会对特定输入执行一系列步骤。应用程序第一次工作,但是如果我第二次尝试用不同的输入值单击按钮,它就会抛出一个错误 我有两个问题: 1.如何修复下面的代码,使其停止抛出此错误消息 二,。我应该添加哪些特定代码,以便在对话框中向最终用户显示任何错误消息(如此消息)?我希望它能够优雅地终止从按钮单击开始抛出错误的整个过程,并用一个新的对话框替换进度条,该对话框给出错误消息,并允许用户单击一个按钮,该按钮将返回到应用程序的主窗口,以便他们可以尝试再次单
import wxversion
import wx
ID_EXIT = 130
class MainWindow(wx.Frame):
def __init__(self, parent,id,title):
wx.Frame.__init__(self,parent,wx.ID_ANY,title, size = (500,500), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
# A button
self.button =wx.Button(self, label="Click Here", pos=(160, 120))
self.Bind(wx.EVT_BUTTON,self.OnClick,self.button)
# the combobox Control
self.sampleList = ['first','second','third']
self.lblhear = wx.StaticText(self, label="Choose TestID to filter:", pos=(20, 75))
self.edithear = wx.ComboBox(self, pos=(160, 75), size=(95, -1), choices=self.sampleList, style=wx.CB_DROPDOWN)
# the progress bar
self.progressMax = 3
self.count = 0
self.newStep='step '+str(self.count)
self.dialog = None
#-------Setting up the menu.
# create a new instance of the wx.Menu() object
filemenu = wx.Menu()
# enables user to exit the program gracefully
filemenu.Append(ID_EXIT, "E&xit", "Terminate the program")
#------- Creating the menu.
# create a new instance of the wx.MenuBar() object
menubar = wx.MenuBar()
# add our filemenu as the first thing on this menu bar
menubar.Append(filemenu,"&File")
# set the menubar we just created as the MenuBar for this frame
self.SetMenuBar(menubar)
#----- Setting menu event handler
wx.EVT_MENU(self,ID_EXIT,self.OnExit)
self.Show(True)
def OnExit(self,event):
self.Close(True)
def OnClick(self,event):
#SECOND EDIT: added try: statement to the next line:
try:
if not self.dialog:
self.dialog = wx.ProgressDialog("Progress in processing your data.", self.newStep,
self.progressMax,
style=wx.PD_CAN_ABORT
| wx.PD_APP_MODAL
| wx.PD_SMOOTH)
self.count += 1
self.newStep='Start'
(keepGoing, skip) = self.dialog.Update(self.count,self.newStep)
TestID = self.edithear.GetValue()
self.count += 1
self.newStep='Continue.'
(keepGoing, skip) = self.dialog.Update(self.count,self.newStep)
myObject=myClass(TestID)
print myObject.description
self.count += 1
self.newStep='Finished.'
(keepGoing, skip) = self.dialog.Update(self.count,self.newStep)
**#FIRST EDIT: Added self.count = 0 on the following line**
self.count = 0
self.dialog.Destroy()
#SECOND EDIT: Added the next seven lines to handle exceptions.
except:
self.dialog.Destroy()
import sys, traceback
xc = traceback.format_exception(*sys.exc_info())
d = wx.MessageDialog( self, ''.join(xc),"Error",wx.OK)
d.ShowModal() # Show it
d.Destroy() #finally destroy it when finished
class myClass():
def __init__(self,TestID):
self.description = 'The variable name is: '+str(TestID)+'. '
app = wx.PySimpleApp()
frame = MainWindow(None,-1,"My GUI")
app.MainLoop()
我的实际代码要复杂得多,但我创建了这个虚拟版本来说明它的问题。当我第一次单击按钮时,上面的代码在python中运行时可以工作。但是,如果我再次尝试单击该按钮,则会在python shell中收到以下错误消息:
Traceback (most recent call last):
File "mypath/GUIdiagnostics.py", line 55, in OnClick
(keepGoing, skip) = self.dialog.Update(self.count,self.newStep)
File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_windows.py", line 2971, in Update
return _windows_.ProgressDialog_Update(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "value <= m_maximum" failed at ..\..\src\generic\progdlgg.cpp(337) in wxProgressDialog::Update(): invalid progress value
在上面
self.dialog.Destroy()
在上面的代码中。但是关于上面代码中错误处理的第二个问题呢?有人能帮我吗?当然,wxpython中的错误处理是一个常见的问题,有已知的方法可以解决。我将使用py2exe将这个应用程序包装成一个exe文件
第二次编辑:我用try:,except:语句回答了上面的第二个问题,该语句是我在上面的代码中添加的。有趣的是,这是第一次没有人能回答我的问题。我是这类编程的新手,所以很多东西我只是在它们出现的时候才开始学习。这个问题现在已经回答了
self.dialog.Destroy()