如何检测是否在 wx 中按下了关闭按钮.文件对话框



如何评估用户是否按下了 Close Window 按钮 打开文件时wx.fileDialog

我有 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.9wx 3.0.2.0

打开文件函数

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 的返回。 如果用户按"确定",则会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

这将确保通过检查返回代码"wx.ID_OK"并使用样式"wx"来销毁 FileDialog,而无需担心。FD_FILE_MUST_EXIST"您知道已选择文件。 如果您需要对取消对话框进行特殊处理,您可以使用"elif"来完成。

最新更新