kp42
kp42

Reputation: 25

How do I add a text entry box within a for loop in wxPython?

I am new to wxPython and working on a basic program with a GUI to let a user label an image. Right now, when the user clicks the "Import" button they can select a directory. The code then displays each image in that directory in a for loop using matplotlib. However, I can't figure out how to access user input in a for loop. This is what the function looks like now:

import matplotlib.pyplot as plt
import io
import os
import wx 

#This function is appended to an "Import" menu item in the frame's menu bar

def OnClick(self, event):
    dlg = wx.DirDialog(self, "Specify folder:", style=wx.DD_DEFAULT_STYLE)
    results = []
    if dlg.ShowModal() == wx.ID_OK:
        folder = dlg.GetPath()
        for image in os.listdir(folder):
            im = plt.imread(image)
            plt.imshow(im)
            buf = io.BytesIO()
            plt.savefig(buf,format='png')
            buf.seek(0)
            self.Image = wx.Image(buf, wx.BITMAP_TYPE_ANY)
            self.Image = wx.StaticBitmap(self, wx.ID_ANY, wx.Bitmap(self.Image))
            self.sizer = wx.BoxSizer(wx.HORIZONTAL)
            self.sizer.Add(self.Image,1,wx.ALIGN_CENTRE)
            self.SetSizer(self.sizer)
            #Getting the input works but it's obviously not part of the GUI
            #Ideally, I would like to create a text box after each image is displayed
            #Then, once the input is submitted, return to the top of the loop
            label = input('Enter label for image:').lower()
            results.append([image, label])

Right now, as a placeholder, I am using input() to get user input in the terminal. That works fine but I would rather have the entry be part of the wxPython GUI. I was wondering if anyone knew of a way to create text entries within the for loop, get the input, then delete/clear that input and get a new one.

I tried:

ipt = wx.TextEntryDialog()
ipt.ShowModal()
result = ipt.GetValue()
ipt.Destroy()

But the box never showed up.

Edit: I'm still curious to see if this is possible. However, the way I ended up moving forward with solving this problem was by opening up a dialog box and taking the input there - rather than creating a new text entry box each time. Because the dialog box opens on top of the image I don't think this is the best solution for a program like the one I described, but others who find this question might find this solution sufficient.

Upvotes: 0

Views: 39

Answers (0)

Related Questions