bind or update list / array within def

Should be easy, but I'm struggling. I have a routine that collects
users responses to questions following selection of a radio button
(Likert type scale) and then clicking on a [Enter Selection] button.
I am trying to store the data to a list [trial #, response, etc.]
following each click (trial). However, even if I make the variables/
list global, wx does not allow me to append (fill) the list inside of
the def __init (self, event) function. Is there a way to get around
this? Can I bind the list to the button event?? Also, I want to write
the data to an output file after clicking a different button
[Finished] following the last trial, but will have the same problem.

Many thanks

Python 2.6.6
wxPython 2.8

What's the def __init (self, event) function? Do you mean def
__init__(self, parent)? Or do you mean the event handler for the
button?

In any case, this should not be a problem. Post a small runnable
sample app that shows the problem:
http://wiki.wxpython.org/MakingSampleApps

Che

···

On Tue, Jan 18, 2011 at 3:19 PM, SDL <asimov1966@gmail.com> wrote:

Should be easy, but I'm struggling. I have a routine that collects
users responses to questions following selection of a radio button
(Likert type scale) and then clicking on a [Enter Selection] button.
I am trying to store the data to a list [trial #, response, etc.]
following each click (trial). However, even if I make the variables/
list global, wx does not allow me to append (fill) the list inside of
the def __init (self, event) function.

Thanks,

I have attached a small sample app with an example. Everything is OK
except the DataList list. I get the same error message in both
windows and linux platforms: "TypeError: 'builtin_function_or_method'
object is unsubscriptable". Here is the code:

import wx
global trial
global click

DataList =
trial = 0
click = 0

class MyFrame(wx.Frame):

    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title,
                          pos=(300, 300), size=(350, 200))

        panel = wx.Panel(self)

        text = wx.StaticText(panel, -1, "Question")
        text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
        # text.SetSize(text.GetBestSize())
        ExitButton = wx.Button(panel, -1, "Exit")
        AnswerButton = wx.Button(panel, -1, "Enter Answer")

        self.Bind(wx.EVT_BUTTON, self.OnExit, ExitButton)
        self.Bind(wx.EVT_BUTTON, self.OnAnswer, AnswerButton)

        # size with border
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(text, 0, wx.ALL, 10)
        sizer.Add(AnswerButton, 0, wx.ALL, 10)
        sizer.Add(ExitButton, 0, wx.ALL, 10)
        panel.SetSizer(sizer)
        panel.Layout()

    def OnExit(self, event):
        self.Close()

    def OnAnswer(self, event):
        global trial
        global click
        trial = trial + 1
        click = click + 2
        print trial, click
        DataList.append[trial, click]

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, "Example App")
        self.SetTopWindow(frame)
        frame.Show(True)
        return True

app = MyApp(redirect=True)
app.MainLoop()

···

On Jan 18, 3:09 pm, C M <cmpyt...@gmail.com> wrote:

On Tue, Jan 18, 2011 at 3:19 PM, SDL <asimov1...@gmail.com> wrote:
> Should be easy, but I'm struggling. I have a routine that collects
> users responses to questions following selection of a radio button
> (Likert type scale) and then clicking on a [Enter Selection] button.
> I am trying to store the data to a list [trial #, response, etc.]
> following each click (trial). However, even if I make the variables/
> list global, wx does not allow me to append (fill) the list inside of
> the def __init (self, event) function.

What's the def __init (self, event) function? Do you mean def
__init__(self, parent)? Or do you mean the event handler for the
button?

In any case, this should not be a problem. Post a small runnable
sample app that shows the problem:MakingSampleApps - wxPyWiki

Che

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks,

I have attached a small sample app with an example. Everything is OK
except the DataList list. I get the same error message in both
windows and linux platforms: "TypeError: 'builtin_function_or_method'
object is unsubscriptable". Here is the code:

import wx
global trial
global click

DataList =
trial = 0
click = 0

class MyFrame(wx.Frame):

def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title,
pos=(300, 300), size=(350, 200))

panel = wx.Panel(self)

text = wx.StaticText(panel, -1, "Question")
text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
# text.SetSize(text.GetBestSize())
ExitButton = wx.Button(panel, -1, "Exit")
AnswerButton = wx.Button(panel, -1, "Enter Answer")

self.Bind(wx.EVT_BUTTON, self.OnExit, ExitButton)
self.Bind(wx.EVT_BUTTON, self.OnAnswer, AnswerButton)

# size with border
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(text, 0, wx.ALL, 10)
sizer.Add(AnswerButton, 0, wx.ALL, 10)
sizer.Add(ExitButton, 0, wx.ALL, 10)
panel.SetSizer(sizer)
panel.Layout()

def OnExit(self, event):
self.Close()

def OnAnswer(self, event):
global trial
global click
trial = trial + 1
click = click + 2
print trial, click
DataList.append[trial, click]

class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, "Example App")
self.SetTopWindow(frame)
frame.Show(True)
return True

app = MyApp(redirect=True)
app.MainLoop()

Should be easy, but I'm struggling. I have a routine that collects
users responses to questions following selection of a radio button
(Likert type scale) and then clicking on a [Enter Selection] button.
I am trying to store the data to a list [trial #, response, etc.]
following each click (trial). However, even if I make the variables/
list global, wx does not allow me to append (fill) the list inside of
the def __init (self, event) function.

What's the def __init (self, event) function? Do you mean def
__init__(self, parent)? Or do you mean the event handler for the
button?

In any case, this should not be a problem. Post a small runnable
sample app that shows the

problem:MakingSampleApps - wxPyWiki

Che

Your problem is in the line:

DataList.append[trial, click]

This should read:

DataList.append((trial, click))

Gadget/Steve

···

On 19/01/2011 4:25 PM, SDL wrote:

On Jan 18, 3:09 pm, C M <cmpyt...@gmail.com> wrote:

On Tue, Jan 18, 2011 at 3:19 PM, SDL <asimov1...@gmail.com> wrote:

.append is a method of your DataList, there it needs () and append only takes exactly one argument, so you could change it to:

DataList.append([trial, click])

Werner

P.S.
Please don't top post and it would be nicer/easier to deal with code by attaching it.

···

On 19/01/2011 17:25, SDL wrote:

Thanks,

I have attached a small sample app with an example. Everything is OK
except the DataList list. I get the same error message in both
windows and linux platforms: "TypeError: 'builtin_function_or_method'
object is unsubscriptable". Here is the code:

import wx
global trial
global click

DataList =
trial = 0
click = 0

class MyFrame(wx.Frame):

     def __init__(self, parent, title):
         wx.Frame.__init__(self, parent, -1, title,
                           pos=(300, 300), size=(350, 200))

         panel = wx.Panel(self)

         text = wx.StaticText(panel, -1, "Question")
         text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
         # text.SetSize(text.GetBestSize())
         ExitButton = wx.Button(panel, -1, "Exit")
         AnswerButton = wx.Button(panel, -1, "Enter Answer")

         self.Bind(wx.EVT_BUTTON, self.OnExit, ExitButton)
         self.Bind(wx.EVT_BUTTON, self.OnAnswer, AnswerButton)

         # size with border
         sizer = wx.BoxSizer(wx.VERTICAL)
         sizer.Add(text, 0, wx.ALL, 10)
         sizer.Add(AnswerButton, 0, wx.ALL, 10)
         sizer.Add(ExitButton, 0, wx.ALL, 10)
         panel.SetSizer(sizer)
         panel.Layout()

     def OnExit(self, event):
         self.Close()

     def OnAnswer(self, event):
         global trial
         global click
         trial = trial + 1
         click = click + 2
         print trial, click
         DataList.append[trial, click]

Thanks,

Others have provided the correction, but I have a few comments. You
said in the first post:

However, even if I make the variables/list global, wx does not allow
me to append (fill) the list inside of the def __init (self, event) function.
Is there a way to get around this?

This suggests that you assumed wxPython was putting a restriction on
what you "should" be able to do. But, later you provided the error
you got:

I have attached a small sample app with an example. Everything is OK
except the DataList list. I get the same error message in both
windows and linux platforms: "TypeError: 'builtin_function_or_method'
object is unsubscriptable".

First, it's good to give the *whole* error message, not just the last
line--or at least the last five or so lines, since it will point out
the offending line in your code.

Next, if you Google "Python TypeError" you can get to the Python docs and find:

exception TypeError
    Raised when an operation or function is applied to an object of
inappropriate type. The associated value is a string giving details
about the type mismatch.

This should tip you off that the problem is not a wxPython problem,
nor anything having to do with Windows or Linux platforms, but a
problem with your code, that is, an error in the use of Python. In
fact, it's a type error, and it tells you what error you've made,
which is you have tried to treat a function--append()--as though it
were a subscriptable object like a list or a tuple in which you can
refer to an item in it with brackets (like mylist[3] is the third item
in mylist).

The point is: don't assume that just because you can't get what you
want that wxPython is preventing a reasonable action. The error
messages are your absolute best friend in debugging your code, which
*will* have many mistakes on your (or anyone's) part. I always find
it much worse when my code doesn't do what I want but runs without
errors--because errors usually quickly show me what I did wrong. It
should give the offending line, too, as I said.

A few other comments on your code:

global trial
global click

Use of globals is generally discouraged in Python, I think because you
are putting names into the global namespace and it can conflict with
names within functions. What you could do instead is to make trial
and click child objects of the frame, so make them self.trial and
self.click. Then they can be accessed from any function within the
Frame's class.

   def OnAnswer(self, event):
       global trial
       global click

I'm not sure (since I never use globals) but isn't this redundant to
declare them again?

       trial = trial + 1
       click = click + 2

That can be, if you prefer (you might know already):

      trial += 1
      click += 1

Che

···

On Wed, Jan 19, 2011 at 11:25 AM, SDL <asimov1966@gmail.com> wrote:

That can be, if you prefer (you might know already):

 trial \+= 1
 click \+= 1

Whoops, the second line there should have been

       click += 2

Che

Good stuff. Tbanks for all your help. New to Python and some of my
issues arise from a long history of coding lab projects in MatLab and
VB (one needs to think differently in Python). Took it on just for
the intellectual challenge; although my learning curve seems rather
flat and my projects are crawling along. I stumbled onto the
DataList() answer before reading the feedback, but the feedback was
more helpful than just solving this little issue. Making the globals
into child objects provided me with good insight for other pieces of
code on which I am working. Good to know there is a responsive
community out there. The flexibility and power of wx will be useful
going forward.

SDL

···

On Jan 19, 11:52 am, C M <cmpyt...@gmail.com> wrote:

> That can be, if you prefer (you might know already):

> trial += 1
> click += 1

Whoops, the second line there should have been

   click \+= 2

Che

Using globals is generally pretty hazardous and dates back to the very early days of high level languages when better ways did not yet exist.

I did a little reorganizing and commenting as well as getting rid of the global statements. See the attached file which shows a suggested app styling.

Ray Pasco

reorganized.py (2.1 KB)

···

On Thu, Jan 20, 2011 at 10:49 AM, SDL asimov1966@gmail.com wrote:

Good stuff. Thanks for all your help. New to Python and some of my

issues arise from a long history of coding lab projects in MatLab and

VB (one needs to think differently in Python). … Good to know there is a responsive

community out there. The flexibility and power of wx will be useful

going forward.