Extracting the value from a TextCtrl inside a SizedDialog

This is my first truly object-oriented project. I've spent over 20
years scripting in shell scripts, some perl, even some procedure-
oriented Python. All command-line and text-based, though. So,
forgive me if the question seems a bit elementary in nature.

Following the demo code for a SizedDialog, I've created several
TextCtrl fields for my users to populate with their own values. I've
managed to create a nice dialog box that looks like I want it to.
Problem is, I can't figure out how to extract the values entered by
the user once they click on the "OK" button. Any attempt to refer to
the TextCtrl.GetValue() either turns up blank or raises an exception
stating that the attribute doesn't exist.

Hi,

This is my first truly object-oriented project. I've spent over 20
years scripting in shell scripts, some perl, even some procedure-
oriented Python. All command-line and text-based, though. So,
forgive me if the question seems a bit elementary in nature.

Following the demo code for a SizedDialog, I've created several
TextCtrl fields for my users to populate with their own values. I've
managed to create a nice dialog box that looks like I want it to.
Problem is, I can't figure out how to extract the values entered by
the user once they click on the "OK" button. Any attempt to refer to
the TextCtrl.GetValue() either turns up blank or raises an exception
stating that the attribute doesn't exist.

Here's a quick and dirty dialog example:

<code>

import wx

class TestDlg(wx.Dialog):

    def __init__(self):
        wx.Dialog.__init__(self, None, wx.ID_ANY, 'Super Simple
Dialog', size=(225,150))

        self.txt = wx.TextCtrl(self, wx.ID_ANY, "")
        self.okBtn = wx.Button(self, wx.ID_OK)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.txt, 0, wx.ALL, 5)
        sizer.Add(self.okBtn, 0, wx.ALL, 5)
        self.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.PySimpleApp()
    dlg = TestDlg()
    result = dlg.ShowModal()
    if result == wx.ID_OK:
        print dlg.txt.GetValue()
    dlg.Destroy()
    app.MainLoop()

</code>

Notice that you have to use the dialog instance variable, "dlg", to
access the dialog's attributes. In this example, I only grab the text
control's value if the user pressed the OK button. If they just hit
the exit button in the upper right, then I would have gotten nothing.

- Mike

···

On Aug 25, 2:32 am, JohnK3 <kemk...@gmail.com> wrote:

Hi John,

This is my first truly object-oriented project. I've spent over 20
years scripting in shell scripts, some perl, even some procedure-
oriented Python. All command-line and text-based, though. So,
forgive me if the question seems a bit elementary in nature.

Following the demo code for a SizedDialog, I've created several
TextCtrl fields for my users to populate with their own values. I've
managed to create a nice dialog box that looks like I want it to.
Problem is, I can't figure out how to extract the values entered by
the user once they click on the "OK" button. Any attempt to refer to
the TextCtrl.GetValue() either turns up blank or raises an exception
stating that the attribute doesn't exist.

Attribute doesn't exist sounds like you're destroying the text control
(or its parent) before accessing it, but I'm not sure why you'd get a
blank value.

Seeing some code might help. :slight_smile:

Regards,

Kevin

···

On Aug 25, 2009, at 12:32 AM, JohnK3 wrote:

>

That did it!

It was the "self" prepended to the text control object that did the
trick. It's kind of obvious, now that I think about it, but last
night, as I was fumbling through things, I just couldn't figure out
how to make it work!

In answer to Kevin's query:

import wx

<code>

class QSODialog(sc.SizedDialog):
    def __init__(self, parent, id):
        sc.SizedDialog.__init__(self, None, -1, "QSO Entry",
style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)

        pane = self.GetContentsPane()
        pane.SetSizerType("form")

        # row 1
        wx.StaticText(pane, -1, "Callsign")
        self.qsoCallSignCtrl = wx.TextCtrl(pane, -1, "")
        self.qsoCallSignCtrl.SetSizerProps(expand=True)

        # row 2
        wx.StaticText(pane, -1, "Name")
        self.qsoNameCtrl = wx.TextCtrl(pane, -1, "")
        self.qsoNameCtrl.SetSizerProps(expand=True)

        # row 3
        wx.StaticText(pane, -1, "Favorite Beverage")
        self.qsoBeverageCtrl = wx.TextCtrl(pane, -1, "")
  self.qsoBeverageCtrl.SetSizerProps(expand=True)

        # row 4
        wx.StaticText(pane, -1, "10-10")
  self.qsoTenTenCtrl = wx.TextCtrl(pane, -1, "")
  self.qsoTenTenCtrl.SetSizerProps(expand=True)

        # add dialog buttons
        self.SetButtonSizer(self.CreateStdDialogButtonSizer(wx.OK |
wx.CANCEL))

        # a little trick to make sure that you can't resize the dialog
to
        # less screen space than the controls need
        self.Fit()
        self.SetMinSize(self.GetSize())

<code>
  def OnQSO(self, event):
    print
    dlg = QSODialog(self, -1)
    dlg.CenterOnScreen()

    # this does not return until the dialog is closed.
    val = dlg.ShowModal()

    if val == wx.ID_OK:
      #--- INSERT CODE TO WRITE QSO TO LOG
      print dlg.qsoCallSignCtrl.GetValue()
      self.OnQSO(event)
      dlg.Destroy()
    else:
      dlg.Destroy()

<code>

That finally got it. Now I know I can retrieve the value, I can start
poking it into a database upon completion of each transaction and
doing other stuff, like checking for duplicates, calculating points,
etc, etc, blahblahblah.

···

On Aug 25, 11:39 am, Mike Driscoll <kyoso...@gmail.com> wrote:

Hi,

On Aug 25, 2:32 am, JohnK3 <kemk...@gmail.com> wrote:

> This is my first truly object-oriented project. I've spent over 20
> years scripting in shell scripts, some perl, even some procedure-
> oriented Python. All command-line and text-based, though. So,
> forgive me if the question seems a bit elementary in nature.

> Following the demo code for a SizedDialog, I've created several
> TextCtrl fields for my users to populate with their own values. I've
> managed to create a nice dialog box that looks like I want it to.
> Problem is, I can't figure out how to extract the values entered by
> the user once they click on the "OK" button. Any attempt to refer to
> the TextCtrl.GetValue() either turns up blank or raises an exception
> stating that the attribute doesn't exist.

Here's a quick and dirty dialog example:

<code>

import wx

class TestDlg(wx.Dialog):

def \_\_init\_\_\(self\):
    wx\.Dialog\.\_\_init\_\_\(self, None, wx\.ID\_ANY, &#39;Super Simple

Dialog', size=(225,150))

    self\.txt = wx\.TextCtrl\(self, wx\.ID\_ANY, &quot;&quot;\)
    self\.okBtn = wx\.Button\(self, wx\.ID\_OK\)

    sizer = wx\.BoxSizer\(wx\.VERTICAL\)
    sizer\.Add\(self\.txt, 0, wx\.ALL, 5\)
    sizer\.Add\(self\.okBtn, 0, wx\.ALL, 5\)
    self\.SetSizer\(sizer\)

if __name__ == "__main__":
app = wx.PySimpleApp()
dlg = TestDlg()
result = dlg.ShowModal()
if result == wx.ID_OK:
print dlg.txt.GetValue()
dlg.Destroy()
app.MainLoop()

</code>

Notice that you have to use the dialog instance variable, "dlg", to
access the dialog's attributes. In this example, I only grab the text
control's value if the user pressed the OK button. If they just hit
the exit button in the upper right, then I would have gotten nothing.

- Mike

The "self" makes the variable into an attribute, property or instance
variable. At least, I think that's the common parlance. Anyway, that
allows you to access that variable via dot notation. It's part of the
whole Object Oriented programming paradigm.

You'll get the hang of it eventually as long as you don't give up.
Once you do, it becomes much easier.

- Mike

···

On Aug 25, 5:42 pm, JohnK3 <kemk...@gmail.com> wrote:

That did it!

It was the "self" prepended to the text control object that did the
trick. It's kind of obvious, now that I think about it, but last
night, as I was fumbling through things, I just couldn't figure out
how to make it work!