Search code examples
pythonwxpythonwxwidgets

How do I create a permanent text entry input on my main wxPython window?


I'm new to wxPython, and I was wondering if it's possible to create a text entry dialog input bar within the main window? Instead of having to create a new box/window for the user input, I'd like to have a permanent input field, like the class resultsInput provides for the PyQt QWidget layout.

I see that it's possible to create Multiple TextEntryDialog windows, but can we do this on a single window?

Is this possible using TextEntryDialog, or do I need to use a different class?

Here's what I have so far:

import wx

class Window(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'WX Python Window', size=(300, 200))
        panel = wx.Panel(self)

        text_enter = wx.TextEntryDialog(None, "Please enter some text.", "Title", "Default")
        if text_enter.ShowModal() == wx.ID_OK:
            response = text_enter.GetValue()
            wx.StaticText(panel, -1, response, (10, 50))

if __name__=='__main__':
    app = wx.PySimpleApp()
    frame = Window(parent=None, id=-1)
    frame.Show()
app.MainLoop()

Thanks in advance!


Solution

  • I was wondering if it's possible to create a text entry dialog input bar

    You seem to be a bit confused here.

    A text entry input bar isn't a dialog. It's just a widget (aka control)—in particular, a TextCtrl.

    A TextEntryDialog includes a TextCtrl, and a title bar, and a caption, and some buttons; you don't want any of that except maybe a button or two. And it has a panel with some sizers to lay things out, but you don't want that; it doesn't even place the buttons alongside the entry bar. It's also a top-level window, which you again don't want. More importantly, it has a bunch of code to act like a dialog—to modally take over the entire interface and not return until the user has typed something and clicked a button—which you definitely don't want.

    So, the only thing you want out of the dialog is that you want a TextCtrl, and a Button, probably organized together in a Panel with a Sizer so you can control the layout. So, just create those. Add a handler for the button that read's the text input's value and does something with it, and you're done.

    (What you're specifically asking for, a way to place a dialog within the window, isn't possible; you can embed top-level windows into child windows, or you can create something that acts just like a dialog but isn't top-level. But neither of those is easy, and neither is what you want here.)