在 wxpython 文件对话框中处理多个条件的正确方法是什么?
What is the proper way to handle multiple conditionals in a wxpython file dialog?
我的 wxpython GUI 有一个打开 FileDialog 的方法:
def open_filedlg(self,event):
dlg = wx.FileDialog(self, "Choose XYZ file", getcwd(), "",
"XYZ files (*.dat)|*.dat|(*.xyz)|*.xyz", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.xyz_source=str(dlg.GetPath())
self.fname_txt_ctl.SetValue(self.xyz_source)
dlg.Destroy()
return
if dlg.ShowModal() == wx.ID_CANCEL:
dlg.Destroy()
return
如果我想取消,我必须按两次 "Cancel" 按钮。如果我颠倒条件的顺序,取消可以正常工作,但我必须按两次 "Open" 按钮才能获取文件名。使用 "elif" 而不是第二个 "if" 不会改变行为。这样做的正确方法是什么?谢谢
问题是,您打开对话框两次(每次 'dlg.ShowModal' 一次)。
试试
dialogStatus = dlg.ShowModal()
if dialogStatus == wx.ID_OK:
...
对于较新版本的 wxPython (2.8.11+),我会使用上下文管理器,如下所示:
def open_filedlg(self,event):
with wx.FileDialog(self, "Choose XYZ file", getcwd(), "",
"XYZ files (*.dat)|*.dat|(*.xyz)|*.xyz", wx.OPEN) as dlg:
dlgResult = dlg.ShowModal()
if dlgResult == wx.ID_OK:
self.xyz_source=str(dlg.GetPath())
self.fname_txt_ctl.SetValue(self.xyz_source)
return
else:
return
#elif dlgResult == wx.ID_CANCEL:
#return
'with' 上下文管理器将自动调用 dlg.destroy()。
我的 wxpython GUI 有一个打开 FileDialog 的方法:
def open_filedlg(self,event):
dlg = wx.FileDialog(self, "Choose XYZ file", getcwd(), "",
"XYZ files (*.dat)|*.dat|(*.xyz)|*.xyz", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.xyz_source=str(dlg.GetPath())
self.fname_txt_ctl.SetValue(self.xyz_source)
dlg.Destroy()
return
if dlg.ShowModal() == wx.ID_CANCEL:
dlg.Destroy()
return
如果我想取消,我必须按两次 "Cancel" 按钮。如果我颠倒条件的顺序,取消可以正常工作,但我必须按两次 "Open" 按钮才能获取文件名。使用 "elif" 而不是第二个 "if" 不会改变行为。这样做的正确方法是什么?谢谢
问题是,您打开对话框两次(每次 'dlg.ShowModal' 一次)。
试试
dialogStatus = dlg.ShowModal()
if dialogStatus == wx.ID_OK:
...
对于较新版本的 wxPython (2.8.11+),我会使用上下文管理器,如下所示:
def open_filedlg(self,event):
with wx.FileDialog(self, "Choose XYZ file", getcwd(), "",
"XYZ files (*.dat)|*.dat|(*.xyz)|*.xyz", wx.OPEN) as dlg:
dlgResult = dlg.ShowModal()
if dlgResult == wx.ID_OK:
self.xyz_source=str(dlg.GetPath())
self.fname_txt_ctl.SetValue(self.xyz_source)
return
else:
return
#elif dlgResult == wx.ID_CANCEL:
#return
'with' 上下文管理器将自动调用 dlg.destroy()。