2013-02-05 32 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