2013-02-05 29 views
1

我有一個可以手動更改的wx.dirPicker控件的Python應用程序,我需要確保在運行我的代碼之前選擇的路徑存在。要做到這一點,我用這個:MessageDialog不關閉

def m_dirPicker1OnUpdateUI(self, event): 
     src_directory = self.m_dirPicker1.GetTextCtrlValue() 
     if os.path.exists(src_directory)==False: 
         dlg = wx.MessageDialog(self, "The specified path doesn't exist", "Warning", wx.ICON_ERROR | wx.ICON_EXCLAMATION) 
         dlg.ShowModal()  
         #print(dlg.GetReturnCode()) 
         if dlg.GetReturnCode() == 0: 
          self.Destroy() 

它工作正常,檢測路徑是否存在。

但是,當路徑不存在時,消息對話框會出現,但按下OK按鈕後無法關閉它,我不明白爲什麼。

謝謝。

回答

0

我想之前你應該叫 「dlg.Destroy()」, 「self.Destroy()」:

result = dlg.ShowModal()  
dlg.Destroy() 
if result == 0: 
    self.Destroy() 
+0

我確實使用過你的代碼,但它也不起作用。我解決了問題,改變了我處理問題的方式 – TMoover

+0

@TMoover:我很高興知道您解決了問題! :)如果您可以在自己的問題中添加答案/評論,並提供關於您如何解決此問題的一些詳細信息,那麼對未來的讀者可能會很好。 – furins

1

我的第一種方法是: 每當有人手動更改wx.dirpicker路徑,我需要以確保路徑存在,因爲我的應用程序會將報告文件導出到該路徑。

後來我決定只有當有人按下「創建報告」按鈕時才檢查路徑。要做到這一點,我使用下面的代碼:

try: 
    if src_directory = self.m_dirPicker1.GetTextCtrlValue(): 
     if os.path.exists(src_directory)==False: 
     dlg = wx.MessageDialog(self, "The specified path doesn't exist", "Warning", wx.ICON_EXCLAMATION) 
     dlg.ShowModal() 
    else: 
     #run my code to create report file in src_directory path 

except: 
    create report_error file