I need a custom dialog. I've been playing around for hours trying to create one and simply don't get it!
All I need is the standard wx.TextEntryDialog but with 2 text fields. Can anyone please help?
Thanks.

Dani AI

Generated

A single modal dialog with two labeled entry controls is the cleanest fix for the “two selections” problem. Instead of stacking two TextEntryDialogs (as was doing), subclass wx.Dialog, put two wx.TextCtrl controls plus OK/Cancel buttons into a sizer, and expose a small GetValues() method. Call ShowModal(), check for wx.ID_OK, read the values, then Destroy() the dialog. This keeps the UI tidy and makes it trivial to pass both values back to the caller.

Example dialog you can drop into your code:

import wx

class TwoFieldDialog(wx.Dialog):
    def __init__(self, parent, title="Two values",
                 label1="Value 1", label2="Value 2",
                 value1="", value2=""):
        super(TwoFieldDialog, self).__init__(parent, title=title)
        lbl1 = wx.StaticText(self, label=label1)
        self.tc1 = wx.TextCtrl(self, value=value1)
        lbl2 = wx.StaticText(self, label=label2)
        self.tc2 = wx.TextCtrl(self, value=value2)

        grid = wx.FlexGridSizer(cols=2, hgap=8, vgap=8)
        grid.AddMany([lbl1, self.tc1, lbl2, self.tc2])

        btn_ok = wx.Button(self, wx.ID_OK)
        btn_cancel = wx.Button(self, wx.ID_CANCEL)
        btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
        btn_sizer.Add((0,0), 1)          # spacer
        btn_sizer.Add(btn_ok, 0, wx.ALL, 5)
        btn_sizer.Add(btn_cancel, 0, wx.ALL, 5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(grid, 0, wx.ALL | wx.EXPAND, 10)
        sizer.Add(btn_sizer, 0, wx.EXPAND | wx.BOTTOM | wx.LEFT | wx.RIGHT, 10)
        self.SetSizerAndFit(sizer)

    def GetValues(self):
        return self.tc1.GetValue(), self.tc2.GetValue()

# usage
dlg = TwoFieldDialog(parent, title="Pick two elements", value1="cobalt")
if dlg.ShowModal() == wx.ID_OK:
    first, second = dlg.GetValues()
dlg.Destroy()

If the user must pick items from a paragraph rather than type them, present the paragraph in a read-only wx.TextCtrl (use GetStringSelection() to capture the highlighted text) or populate a wx.ListBox and allow multi-selection. For list-based selection use wx.LB_MULTIPLE or wx.LB_EXTENDED depending on whether you want toggle behavior or shift/ctrl selection (see the wx.ListBox and wx.TextCtrl docs for details). See the wx.Dialog reference for modal patterns and wx.Validator if you later want automatic validation or transfer logic: wx.Dialog docs, wx.ListBox docs, wx.TextCtrl docs.

This approach gives a single, reusable dialog that returns both values cleanly and avoids the UI awkwardness of back-to-back single-entry dialogs.

Recommended Answers

All 4 Replies

2 text fields requires 2 Entry (Dialogs). Although TextCtrl is generally used. A standard example for entering user name and password.

import wx

class TextFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Text Entry Example', size=(300, 100))
        panel = wx.Panel(self, -1) 
        basicLabel = wx.StaticText(panel, -1, "Basic Control:")
        basicText = wx.TextCtrl(panel, -1, "I've entered some text!", size=(175, -1))
        basicText.SetInsertionPoint(0)

        pwdLabel = wx.StaticText(panel, -1, "Password:")
        pwdText = wx.TextCtrl(panel, -1, "password", size=(175, -1),style=wx.TE_PASSWORD)
        sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
        sizer.AddMany([basicLabel, basicText, pwdLabel, pwdText])
        panel.SetSizer(sizer)

app = wx.PySimpleApp()
frame = TextFrame()
frame.Show()
app.MainLoop()

I have created a similar frame as a child window with text boxes but I don't understand how to get the information from this to the main program, hence the question about modifying, so I can see whats going on and try to expand on it.

I have a large paragraph of text and the user has to pick two elements from it. I can do it with a standard textentrydialog for just one element:

dlg = wx.TextEntryDialog(self, paragraph,'Please choose an element...')
        dlg.SetValue("cobalt")

Until now I've had a second textentrydialog open immediately after the first but it is ugly!

There is not something similar than wxLB_MULTIPLE for ListBox?

I'm sorry to sound ungrateful but from the questions I'm asking it must be clear that I am an absolute beginner. So, single line questions to my questions, phrased in the style of Yoda are not going to ease my torment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.