【问题标题】:How to detect if close button was pressed in a wx.FileDialog?如何检测是否在 wx.FileDialog 中按下了关闭按钮?
【发布时间】:2015-07-15 11:28:10
【问题描述】:

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

我有 2 个文本框... 一个用于显示输入文件路径 另一个是显示输出文件路径

output 依赖于input filepath,因为output filepath 将与输入的目录相同,但名称不同。

有时用户opens the filepath and press the close button... 并且该操作会生成一个等于_edited.txtoutfile,但这不应该发生。

我想要类似的东西

if user_action == press_the_close_button:
    outfile = ""
else:
    outfile = infile_edited.txt

我该如何解决?

我正在使用: Py 2.7.9wx 3.0.2.0

openFile 函数

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"

【问题讨论】:

    标签: python python-2.7 wxpython wxwidgets


    【解决方案1】:

    您需要检查 wxFileDialog::ShowModal 的返回。如果用户按下 OK 则为 wxID_OK,否则为 wxID_CANCEL。

    http://docs.wxwidgets.org/trunk/classwx_file_dialog.html#a04943e4abb27a197a110898d40ddb4f0

    【讨论】:

      【解决方案2】:

      从 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
      

      这将确保 FileDialog 将被销毁,而您不必担心,通过检查返回代码“wx.ID_OK”并使用样式“wx.FD_FILE_MUST_EXIST”,您知道选择了一个文件。如果您需要对取消对话框进行特殊处理,您可以使用 'elif' 来实现。

      【讨论】:

        猜你喜欢
        • 2015-10-30
        • 2012-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-11
        • 2019-04-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多