我有一个wxpython应用程序,当您单击某个按钮时,它会对特定输入执行一系列步骤。应用程序第一次运行,但是如果我尝试用不同的输入值再次单击按钮,它会抛出一个错误。在
我有两个问题: 1.)如何修复下面的代码,使其停止抛出此错误消息?在
2.)我应该添加哪些特定的代码,以便在对话框中向最终用户显示类似此错误消息?我希望它能够优雅地终止抛出错误的整个进程(从单击按钮开始),并用一个新的对话框替换进度条,该对话框给出错误消息,并允许用户单击一个按钮,将他们带回应用程序的主窗口,以便他们可以再次尝试单击按钮它与新的输入一起工作。我很快就要把它打包成一个exe文件,但是现在我唯一的错误处理方法就是pythonshell中的错误消息。由于用户没有pythonshell,所以我需要以本段中描述的方式显示错误消息。注意,这里的OnClick()函数实例化了一个类。我在实际代码中使用了许多不同的类,每个类都非常复杂,我需要这里所要求的功能,以便能够处理由OnClick()函数实例化的各种类的内部深处发生的错误。在
下面是我的代码的一个非常简化但有效的版本:
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中会收到以下错误消息:
^{pr2}$第一次编辑:好的,我通过添加
self.count = 0
以上
self.dialog.Destroy()
在上面的代码中。但是关于上面代码中错误处理的第二个问题呢?有人能帮我吗?当然,wxpython中的错误处理是一个常见的问题,有一些已知的方法。我将使用py2exe将这个应用程序打包成一个exe文件。在
第二次编辑:我用try:回答了上面的第二个问题,除了:我在上面的代码中添加了语句。有趣的是,这是第一次堆栈溢出上没有人能够回答我的问题。我对这种类型的编程还不熟悉,所以很多事情我都是顺其自然的。这个问题现在得到了回答。在
目前没有回答
相关问题 更多 >
编程相关推荐