I'm using wxPython to build some GUI... Indeed it isn't an easy program...
Many many user inputs and outputs to be generated...
One part of the program I put an "Add" button that will dynamically add a TextCtrl field and an "Open" button to open a file. After clicking in the "Open" the user can select a file, the file pathway is therefore showed in the TextCtrl field.
Indeed, using a simple example (one TextCtrl one button) I can handle it...
But in a dynamically way, putting several TextCtrl and several Buttons I don't know how to handle it...
In the following code (only a small part of all, some stuff should not be there), I put a "def OpenReadFile" as you can see there after "Open" button click, it will put the text in the last TextCtrl not in the corresponding field... Any ideas?
In another words... Imagine this: Add (the user can Add "n" samples)
"TEXT-1" 'BUTTON-1'
"TEXT-2" 'BUTTON-2'
"TEXT-3" 'BUTTON-3'
if the user click in button-3 for example it will put the text in Text-3 field (as expected)
if the user click in button-2, it will put the text in Text-3...
My code so far (Indeed I know where the mistake is, I just don't know what to d0) =[
self.addButton = wx.Button(self, label="Add Sample")
self.Bind(wx.EVT_BUTTON, self.OnAddWidget, self.addButton)
def OnAddWidget(self, event):
self.samplenumber += 1
self.sampleTextCtrl = wx.TextCtrl(self, wx.NewId(), "", size = (200,-1))
self.buttonF = wx.Button(self, buttonId, label = "Select File") #Add Open File Button
self.Bind(wx.EVT_BUTTON, self.OpenReadFile, self.buttonF) #Add click event
self.fgs4.Add(self.buttonF, proportion = 1, flag = wx.CENTER, border = -1)
def OpenReadFile(self,event):
dlg = wx.FileDialog(self, "Open File",
os.getcwd(), style = wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetPath()
self.sampleTextCtrl.SetValue(self.filename)
I'm pretty sure that I do have to do some stuff in OpenReadFile =]
EDITED
I've put a dict before, got the button ID and the TextCtrl put it in the dict and done.
my_dict = {}
self.sampleTextCtrl = wx.TextCtrl(self, wx.NewId(), "", size = (200,-1))
value = self.sampleTextCtrl
buttonId = wx.NewId()
self.buttonF = wx.Button(self, buttonId, label = "Select File")
mydict[buttonId] = value
Done