2011-01-28 86 views
2

我有一個wx python應用程序,當你點擊一個按鈕時,它執行一系列特定輸入的步驟。該應用程序第一次運行,但如果我嘗試再次單擊該按鈕時使用不同的輸入值,則會引發錯誤。WXPython錯誤處理和重複使用

我有兩個問題: 1.)我如何修復我的代碼,以便它不會拋出此錯誤消息?

2.)我應該添加哪些特定的代碼,以便在對話框中向最終用戶顯示諸如此類的任何錯誤消息?我希望它能夠優雅地殺掉拋出錯誤的整個過程(從按鈕單擊開始),並用一個新的對話框替換進度條,該對話框給出錯誤消息,並允許用戶單擊一個按鈕他們回到應用程序的主窗口,以便他們可以嘗試再次單擊該按鈕以使其與新輸入一起工作。我即將把它包裝在exe文件中,但現在我唯一的錯誤處理是python shell中的錯誤信息。由於用戶不會擁有python shell,我需要按照本段所述的方式創建錯誤消息。注意我的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中出現以下錯誤信息:

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 

先編輯: OK,我回答我上面的第一個問題通過增加

self.count = 0 

上述

self.dialog.Destroy() 
在上面的代碼

。但是關於上面代碼中錯誤處理的第二個問題呢?任何人都可以幫助我嗎?當然,wxpython中的錯誤處理是已知方法的常見問題。我將使用py2exe將此應用程序包裝到一個exe文件中。


第二個編輯:我回答我的第二個問題上面的嘗試:除了:我已經添加到我的代碼上面的語句。有趣的是,這是第一次,堆棧溢出沒有人能夠回答我的問題。我對這種類型的編程是新手,所以很多事情我只是在他們來的時候纔會採用。現在回答這個問題。

回答

0

我現在回答了我的兩個問題(由我參見上文)。所以這個線程現在關閉了。