Search code examples
pythonwxpython

how to get a variable out of wxpython file open


I am trying to get a variable out of wxPython file open dialog. I have 2 button which gets the path to 2 files with .GetPath() ?

this is the code I have so far

'def onclk1(event):
with  wx.FileDialog(panel, "OPEN EMG FILE", wildcard="TXT Files(*.txt)|*.txt",
                    style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as EmgFile:
    if EmgFile.ShowModal() == wx.ID_CANCEL:
        return "cancelled"
    emg = EmgFile.GetPath()

e1.Bind(wx.EVT_BUTTON, onclk1)

Now I need to pass the path outside def to another variable.

Thanks in advance


Solution

  • Just to provide another option, there is also a wx.FilePickerCtrl

    import wx
    import os
    
    wildcard = "All files (*.*)|*.*"
    
    
    class MainWindow(wx.Frame):
    
        def __init__(self):
            wx.Frame.__init__(self, None, title='File Selector')
            self.currentDirectory = os.getcwd()
    
            self.panel = wx.Panel(self)
            vbox = wx.BoxSizer(wx.VERTICAL)
    
            ie_box = wx.StaticBox(self.panel, -1, 'Please select the input file')
            ie_sizer = wx.StaticBoxSizer(ie_box, wx.VERTICAL)
    
            fl_box = wx.BoxSizer(wx.HORIZONTAL)
            self.fl_ctrl = wx.FilePickerCtrl(self.panel, message="Choose a file")
            fl_box.Add(self.fl_ctrl, 1, wx.ALL | wx.CENTER | wx.EXPAND, 5)
            ie_sizer.Add(fl_box, 1, wx.ALL | wx.CENTER | wx.EXPAND, 10)
            self.fl_ctrl.Bind(wx.EVT_FILEPICKER_CHANGED, self.on_open_file)
    
            vbox.Add(ie_sizer, 0, wx.ALL | wx.CENTER | wx.EXPAND, 5)
            self.panel.SetSizer(vbox)
            self.Center()
            self.panel.Fit()
            self.Show()
    
        def on_open_file(self, event):
            self.fl_ctrl.GetPath()
    
    
    if __name__ == '__main__':
        app = wx.App()
        frame = MainWindow()
        app.MainLoop()
    

    enter image description here