0
votes

I am trying to create a basic dialog to edit a settings file. I have some list of necessary files

options = ["dog", "cat", "chicken"]

I then have a column with the option names in StaticText and a column of TextCtrl to input the file location. Next to each TextCtrl is a "Select File" button.

How can I have each button select a file and then update the TextCtrl with the location? At the moment, I iterate through the options list to generate the text and buttons. I know if I were to define everything by hand, I could have a handler unique to each button which updates the the corresponding TextCtrl, but I feel like there is a better when. I would like it to be modifiable so I can just add an option to the options list, and another row of options is added.

Currently I am binding each button to self.OnSelect, but I'm not sure how to use that to update the correct TextCtrl. To reiterate I know how to use FileDialog, I am just having troubles tying each button to its corresponding TextCtrl.

selectBtn = wx.Button(self, wx.ID_FILE, label="Select File")
selectBtn.Bind(wx.EVT_BUTTON, self.OnSelect)

Should I have a unique ID instead of using wx.ID_FILE?

I am relatively new to GUI desgin so any help is appreciated. Even if you think what I'm doing may be ugly and something else would be better, please let me know

1
Well I posted a little too soon. A very similar problem was posted here: stackoverflow.com/questions/32512376/… - Shatnerz

1 Answers

0
votes

My answer was derived from here: wxPython how to put a text in TextCtrl with a button inserted by "Add" Button

By using lambda to create anonymous functions, the solution is quite simple. You can have a handler, like:

def OnSelect(self, event, ctrl):
    name, path = selectFile() #gets name and path using FileDialog
    ctrl.SetValue(path)

The buttons can be bound like

selectBtn = wx.Button(self, wx.ID_FILE, label="Select File")
selectEvent = lambda event, ctrl=txt: self.OnSelect(event, ctrl)
selectBtn.Bind(wx.EVT_BUTTON, selectEvent)

Works like a charm.