如何检测 wx.FileDialog 中是否按下了关闭按钮?
How to detect if close button was pressed in a wx.FileDialog?
How can i evaluate if a user pressed the Close Window
button in a
wx.fileDialog
while opening a file?
我有 2 个文本框...
一个用于显示输入文件路径
另一个是显示输出文件路径
output
取决于 input filepath
,因为 output filepath
将与输入目录相同,但名称不同。
有时用户 opens the filepath and press the close button
...
并且该操作生成 outfile
等于 _edited.txt
但这不应该发生。
我想要
if user_action == press_the_close_button:
outfile = ""
else:
outfile = infile_edited.txt
我该如何解决?
我正在使用:
Py 2.7.9 和 wx 3.0.2.0
the openFile function
def abrirArquivo(self, event):
try:
#open fileDialog box
openFileDialog = wx.FileDialog(self, "Open", "", "", "Text files (*.txt)|*.txt", wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
openFileDialog.ShowModal()
#get the filepath
infile = openFileDialog.GetPath()
#change the inputTxtCtrl to the filepath
self.inputTxtCtrl.SetValue(infile)
#if user dont select any file in the fileDialog, how can i evaluate it and correctly so it doesnt change the outfileTextBox
if infile == "":
#change textLabel to "File not selected.."
self.textoProgresso.SetLabel("File wasn't loaded properly...")
else:
#create output filepath
outfile = infile[0:len(str(infile))-4] + "_edited.txt"
#change outputTxtCtrl to the outfile filepath
self.outputTxtCtrl.SetValue(outfile)
#change progress bar to 10%
self.barraProgresso.SetValue(10)
#change textLabel to "File found.."
self.textoProgresso.SetLabel("File loaded in the system...")
except Exception:
print "error_abrir_arquivo"
您需要检查 wxFileDialog::ShowModal 中的 return。如果用户按下确定,它将是 wxID_OK,否则将是 wxID_CANCEL。
http://docs.wxwidgets.org/trunk/classwx_file_dialog.html#a04943e4abb27a197a110898d40ddb4f0
从 wxPython 2.8.11 开始,您可以使用上下文管理器。
with wx.FileDialog(self,
message='Choose file to open',
defaultDir=wx.GetHomeDir(),
defaultFile='evaluation.doc',
style=wx.FD_OPEN|wx.FD_FILE_MUST_EXIST) as dlgF:
fdRet = dlgF.ShowModal()
if fdRet == wx.ID_OK:
fileName = dlgF.GetPath()
# do something with the file e.g.
desktop.open(fileName)
elif fdRet == wx.ID_CANCEL:
# optional handling of cancel button
pass
通过检查 return 代码 "wx.ID_OK" 并使用样式 "wx.FD_FILE_MUST_EXIST" 您知道一个文件被选中。如果您需要对取消对话框进行特殊处理,您可以使用 'elif'.
How can i evaluate if a user pressed the
Close Window
button in awx.fileDialog
while opening a file?
我有 2 个文本框... 一个用于显示输入文件路径 另一个是显示输出文件路径
output
取决于 input filepath
,因为 output filepath
将与输入目录相同,但名称不同。
有时用户 opens the filepath and press the close button
...
并且该操作生成 outfile
等于 _edited.txt
但这不应该发生。
我想要
if user_action == press_the_close_button:
outfile = ""
else:
outfile = infile_edited.txt
我该如何解决?
我正在使用: Py 2.7.9 和 wx 3.0.2.0
the openFile function
def abrirArquivo(self, event):
try:
#open fileDialog box
openFileDialog = wx.FileDialog(self, "Open", "", "", "Text files (*.txt)|*.txt", wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
openFileDialog.ShowModal()
#get the filepath
infile = openFileDialog.GetPath()
#change the inputTxtCtrl to the filepath
self.inputTxtCtrl.SetValue(infile)
#if user dont select any file in the fileDialog, how can i evaluate it and correctly so it doesnt change the outfileTextBox
if infile == "":
#change textLabel to "File not selected.."
self.textoProgresso.SetLabel("File wasn't loaded properly...")
else:
#create output filepath
outfile = infile[0:len(str(infile))-4] + "_edited.txt"
#change outputTxtCtrl to the outfile filepath
self.outputTxtCtrl.SetValue(outfile)
#change progress bar to 10%
self.barraProgresso.SetValue(10)
#change textLabel to "File found.."
self.textoProgresso.SetLabel("File loaded in the system...")
except Exception:
print "error_abrir_arquivo"
您需要检查 wxFileDialog::ShowModal 中的 return。如果用户按下确定,它将是 wxID_OK,否则将是 wxID_CANCEL。
http://docs.wxwidgets.org/trunk/classwx_file_dialog.html#a04943e4abb27a197a110898d40ddb4f0
从 wxPython 2.8.11 开始,您可以使用上下文管理器。
with wx.FileDialog(self,
message='Choose file to open',
defaultDir=wx.GetHomeDir(),
defaultFile='evaluation.doc',
style=wx.FD_OPEN|wx.FD_FILE_MUST_EXIST) as dlgF:
fdRet = dlgF.ShowModal()
if fdRet == wx.ID_OK:
fileName = dlgF.GetPath()
# do something with the file e.g.
desktop.open(fileName)
elif fdRet == wx.ID_CANCEL:
# optional handling of cancel button
pass
通过检查 return 代码 "wx.ID_OK" 并使用样式 "wx.FD_FILE_MUST_EXIST" 您知道一个文件被选中。如果您需要对取消对话框进行特殊处理,您可以使用 'elif'.