Gui, bind, args

Ah HA! I finally managed to get subscribed....tried to use the web
link, and was getting rejected cause I was spam apparantly.

I'm new, so let's get that out of the way. If there's a list for that,
I'll go there, but tutor-python sent me here.

Building a gui app using wxPython, (yay wxPython!) and I've got some
questions about event handling. I've encountered two different ways it
seems of registering events:

self.Bind(wx.EVT_TEXT, self.EvtText, cb)

and

EVT_TEXT(self, ID_BUTTON1, self.OnButton)

Is there a difference between these two methods? And how is argument
handling done when passing too a function this way. I've managed to get
my notebook working, I've set up the fields for data entry, tagged
everything with an ID, but when it comes time to hit the 'add item'
button, I'm not sure how to pass the entries to my write method.
(either a text file or an SQL handler, haven't decided which yet.) Do I
have to create everything as a self. reference for each window in the
notebook? Or can the scope remain strictly local to the notebook
object?
Did that make sense?

If there are any sadists out there, I can show you the code as it is
now. It could be better, I'm sure of it, but I'm still new to builiding
really elegent fucntions etc...stupid self taught...:slight_smile:

And as an aside, is there a way to 'disable' a menu item? I didn't see
anything in the docs.

Stryder

firephreek wrote:

I'm new, so let's get that out of the way. If there's a list for that,
I'll go there, but tutor-python sent me here.

you're in the right place.

self.Bind(wx.EVT_TEXT, self.EvtText, cb)

and

EVT_TEXT(self, ID_BUTTON1, self.OnButton)

The first is the "new" way, and the second is the old way. They do the same thing, I think it's a matter of taste. I'd use the new one, as I like it better, and the old way may be deprecated at some point.

There have been a number of changes to the API (and standard practice) in the last couple versions, many of the old ways are kept around for backward compatibility, and example are in a wide mix of styles, so it can be confusing. The above is one, and here's a couple others:

use: import wx
and: wx.Something, rather than wxSomething

In general, don't hard code IDs. pass in -1, and one will be generated, then get the ID, if you need it (most often you don't), with GetId(). IF you have a need to share Ids, create one with wx.NewId().

> And how is argument

handling done when passing too a function this way.

the method gets called with the event object as an argument, so you want to write you handler so:

def OnButton(self, event):
    .........

event has a lot of useful information in it, though not much in the case of a button press.

'add item'
button, I'm not sure how to pass the entries to my write method.
(either a text file or an SQL handler, haven't decided which yet.) Do I
have to create everything as a self. reference for each window in the
notebook? Or can the scope remain strictly local to the notebook
object?
Did that make sense?

maybe, I usually have a class for each page in the notebook, maybe subclassed from the same superclass, if they are all similar. IN that class, you create your controls like so:

self.NameBox = wx.TextCtrl(self, -1, "DefaultName")

in the handler, for the "submit" button, you can then get the text like so:

self.NameBox.GetValue()

If there are any sadists out there, I can show you the code as it is
now. It could be better, I'm sure of it, but I'm still new to builiding
really elegent fucntions etc...stupid self taught...:slight_smile:

It can be very helpful to post complete code here, particularly if something isn't quite working how you want it to. But keep it small,a nd ideally, a self contained app that we can run. Post it as an enclosure, so your mailer doesn't mangle the text.

And as an aside, is there a way to 'disable' a menu item? I didn't see
anything in the docs.

wx.MenuItem.Enable(False)

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Ah HA! I finally managed to get subscribed....tried to use the web
link, and was getting rejected cause I was spam apparantly.

I'm new, so let's get that out of the way. If there's a list for that,
I'll go there, but tutor-python sent me here.

Building a gui app using wxPython, (yay wxPython!) and I've got some
questions about event handling. I've encountered two different ways it
seems of registering events:

self.Bind(wx.EVT_TEXT, self.EvtText, cb)

and

EVT_TEXT(self, ID_BUTTON1, self.OnButton)

well, I'm not trying to screw with your mind but there is a third method: the event manager (see demo)
and a small variation on the Bind, like this:

cb.Bind(wx.EVT_TEXT, self.EvtText)

assuming that cb generates wx.EVT_TEXT, this variant is the easiest as it does not needs the ID of the component.

To access data incapsulate it, like instead of
cb = wx.TextCtrl(self, -1, "")
do
self.cb = wx.TextCtrl(self, -1, "")
this way you'll have access to that component in the event handler, you'll be able to say
self.cb.GetValue()

If there are any sadists out there, I can show you the code as it is
now. It could be better, I'm sure of it, but I'm still new to builiding
really elegent fucntions etc...stupid self taught...:slight_smile:

I don't know if sadist is the right word (pleasure in being cruel) maybe masochist (the act of turning one's destructive tendencies inward or upon oneself) :smiley: Anyway... send the code on my address and I'll take a look, maybe give some pointers. :wink:

···

On Thu, 6 May 2004 11:43:55 -0700, firephreek <firephreek@earthlink.net> wrote:

Stryder

--
Peter Damoc
Hacker Wannabe

firephreek wrote:

Ah HA! I finally managed to get subscribed....tried to use the web
link, and was getting rejected cause I was spam apparantly.

I'm new, so let's get that out of the way. If there's a list for that,
I'll go there, but tutor-python sent me here.

Building a gui app using wxPython, (yay wxPython!) and I've got some
questions about event handling. I've encountered two different ways it
seems of registering events:

self.Bind(wx.EVT_TEXT, self.EvtText, cb)
       

and

EVT_TEXT(self, ID_BUTTON1, self.OnButton)
       
Is there a difference between these two methods? And how is argument
handling done when passing too a function this way. I've managed to get
my notebook working, I've set up the fields for data entry, tagged
everything with an ID, but when it comes time to hit the 'add item'
button, I'm not sure how to pass the entries to my write method.
(either a text file or an SQL handler, haven't decided which yet.) Do I
have to create everything as a self. reference for each window in the
notebook? Or can the scope remain strictly local to the notebook
object?
Did that make sense?

You might want to look at validators to fill your controls and get the data from the control to SQL.

···

If there are any sadists out there, I can show you the code as it is
now. It could be better, I'm sure of it, but I'm still new to builiding
really elegent fucntions etc...stupid self taught...:slight_smile:

And as an aside, is there a way to 'disable' a menu item? I didn't see
anything in the docs.

Stryder

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Thanks for the help guys. The different ways to bind events is kinda
nice. Different ways to restructure the program. I like the newer ways
more. They make more sense, and seem to flow a little better.

Still haven't come up with a perfect solution yet though. What I want
to do is to create an event that can pass arguments to a function that
it calls. From what I can see so far, all that gets passed, or is
passable to a function from an event is the event itself. The only
other option I have is to give my variables a global scope, but then the
function has to name them explicitly, and this keeps them from being
fully dynamic. I would like to have a couple of reusable functions that
do similar things across the entire program. Is this possible? Am I
missing something?

I've attached a copy of the src this time, maybe it'll make sense.

And yeah, masochist, not sadist.

Stryder

SNC_dataEntryControl.py (11.3 KB)

···

-----Original Message-----
From: Peter Damoc [mailto:pdamoc@gmx.net]
Sent: Thursday, May 06, 2004 10:28 PM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] Gui, bind, args

On Thu, 6 May 2004 11:43:55 -0700, firephreek <firephreek@earthlink.net> wrote:

Ah HA! I finally managed to get subscribed....tried to use the web
link, and was getting rejected cause I was spam apparantly.

I'm new, so let's get that out of the way. If there's a list for
that, I'll go there, but tutor-python sent me here.

Building a gui app using wxPython, (yay wxPython!) and I've got some
questions about event handling. I've encountered two different ways
it seems of registering events:

self.Bind(wx.EVT_TEXT, self.EvtText, cb)

and

EVT_TEXT(self, ID_BUTTON1, self.OnButton)

well, I'm not trying to screw with your mind but there is a third
method:
the event manager (see demo)
and a small variation on the Bind, like this:

cb.Bind(wx.EVT_TEXT, self.EvtText)

assuming that cb generates wx.EVT_TEXT, this variant is the easiest as
it
does not needs the ID of the component.

To access data incapsulate it, like instead of
cb = wx.TextCtrl(self, -1, "")
do
self.cb = wx.TextCtrl(self, -1, "")
this way you'll have access to that component in the event handler,
you'll
be able to say
self.cb.GetValue()

If there are any sadists out there, I can show you the code as it is
now. It could be better, I'm sure of it, but I'm still new to
builiding really elegent fucntions etc...stupid self taught...:slight_smile:

I don't know if sadist is the right word (pleasure in being cruel) maybe

masochist (the act of turning one's destructive tendencies inward or
upon
oneself) :smiley: Anyway... send the code on my address and I'll take a look,

maybe give some pointers. :wink:

Stryder

--
Peter Damoc
Hacker Wannabe

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

firephreek wrote:

Still haven't come up with a perfect solution yet though. What I want
to do is to create an event that can pass arguments to a function that
it calls.

You can create custom events to do this, but that gets pretty ugly.

The only
other option I have is to give my variables a global scope,

I've attached a copy of the src this time, maybe it'll make sense.

You need to modularize your code more. your input panel should be its own class. It could take various data as input to the __init__ that would tell it how to set itself up. That way, your "global" scope would only be the class instance scope--much more contained.

As for using general purpose functions that can be used by various classes in your program, yes, there are ways to do that. We'd need a more specific example of what you have in mind in order to make suggestions.

Python is very dynamic, and thus very well suited to dynamically creating forms to match data. There are times when it would be nice to add a "userdata" parameter to an event binding, but you can accomplish the same thing with an extra layer of indirection. Here's some untested pseudo code that might do something like you want:

class MyPanel(wx.Panel):
     def __init__(self, SetUpData):
         wx.Panel.__init__( ......

         self.SetUpData = SetUpData
         self.ButtonDict = {}
         for button in SetUpData.Buttoninfo:
             b = wx.Button(self, -1, button.name)
             self.ButtonDict[b.GetId()] = button
             b.Bind(wx.EVT_BUTTON, self.OnButton)
             ........
             DoSomethingWithSizers
             ........

..........

     def OnButton(self,event):
  button = self.ButtonDict[event.GetEventObject().GetId()]
  ..........
         Do whatever you want with button, which can have any data in it.
         Or even call a method of the button object:

  button.DoTheThing()
  
         ........

This now gives you a custom class instance, that has any number of button in it, and each can do a different thing, depending on what is in SetUpData.ButtonInfo, and the button objects it contains. The button objects can be complex, of just have some simple data fields in them.

For a more trivial example of a bunch of wx.Panels created dynamically, see the enclosed app.

-Chris

Converter.py (10.9 KB)

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Hi all,

I'm getting the following warning when my app starts up:

(process:5199): Gdk-WARNING **: locale not supported by C library

(process:5199): Gtk-WARNING **: Locale not supported by C library.
         Using the fallback 'C' locale.

it seems to be happening in the wx.App.__incit__, before OnInit is called.

The strange part is that it doesn't happen with another my wxPython apps. The only difference I can see is that I call wx.InitAllImageHandlers in the one with the errors, bu that's in OnInit, and the errors seem to come before that.

Anyone have any ideas?

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Wow! Thank you so much! That gives me a good bit to chew on...

I recognize my code needs to be more modular, I'm just not that good at
breaking it down yet. I overanalyze myself till I'm standing still.
Python is my first langauge, so it's slow going at times it feels like.
I appreciate the comments.

Going through the code, I noticed the reference to 'ConvertDataUnits'.
I didn't realize you could reference a variable from a class like
that. I thought you had to declare it global. Does this mean any
variable at the top layer is automatically a global? I guess that makes
sense. Guess I just never noticed it.

Stryder

···

-----Original Message-----
From: Chris Barker [mailto:Chris.Barker@noaa.gov]
Sent: Friday, May 07, 2004 12:25 PM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] Gui, bind, args

firephreek wrote:

Still haven't come up with a perfect solution yet though. What I want

to do is to create an event that can pass arguments to a function that

it calls.

You can create custom events to do this, but that gets pretty ugly.

The only
other option I have is to give my variables a global scope,

I've attached a copy of the src this time, maybe it'll make sense.

You need to modularize your code more. your input panel should be its
own class. It could take various data as input to the __init__ that
would tell it how to set itself up. That way, your "global" scope would
only be the class instance scope--much more contained.

As for using general purpose functions that can be used by various
classes in your program, yes, there are ways to do that. We'd need a
more specific example of what you have in mind in order to make
suggestions.

Python is very dynamic, and thus very well suited to dynamically
creating forms to match data. There are times when it would be nice to
add a "userdata" parameter to an event binding, but you can accomplish
the same thing with an extra layer of indirection. Here's some untested
pseudo code that might do something like you want:

class MyPanel(wx.Panel):
     def __init__(self, SetUpData):
         wx.Panel.__init__( ......

         self.SetUpData = SetUpData
         self.ButtonDict = {}
         for button in SetUpData.Buttoninfo:
             b = wx.Button(self, -1, button.name)
             self.ButtonDict[b.GetId()] = button
             b.Bind(wx.EVT_BUTTON, self.OnButton)
             ........
             DoSomethingWithSizers
             ........

..........

     def OnButton(self,event):
  button = self.ButtonDict[event.GetEventObject().GetId()]
  ..........
         Do whatever you want with button, which can have any data in
it.
         Or even call a method of the button object:

  button.DoTheThing()
  
         ........

This now gives you a custom class instance, that has any number of
button in it, and each can do a different thing, depending on what is in

SetUpData.ButtonInfo, and the button objects it contains. The button
objects can be complex, of just have some simple data fields in them.

For a more trivial example of a bunch of wx.Panels created dynamically,
see the enclosed app.

-Chris

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

oops:

Gentoo Linux, wxPython 2.5.1, GTK2

Chris Barker wrote:

···

Hi all,

I'm getting the following warning when my app starts up:

(process:5199): Gdk-WARNING **: locale not supported by C library

(process:5199): Gtk-WARNING **: Locale not supported by C library.
        Using the fallback 'C' locale.

it seems to be happening in the wx.App.__incit__, before OnInit is called.

The strange part is that it doesn't happen with another my wxPython apps. The only difference I can see is that I call wx.InitAllImageHandlers in the one with the errors, bu that's in OnInit, and the errors seem to come before that.

Anyone have any ideas?

-Chris

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

firephreek wrote:

Going through the code, I noticed the reference to 'ConvertDataUnits'.
I didn't realize you could reference a variable from a class like
that. I thought you had to declare it global. Does this mean any
variable at the top layer is automatically a global? I guess that makes
sense. Guess I just never noticed it.

You have to declare it global If you want to re-bind it inside another scope. i.e.:

x = 5

def test(y):
  z = y + x # you can reference x here
  return z

def test2(y):
  x = y # you can not re-bind it!
  return z

def test2(y):
  global x
  x = y # you can re-bind it now.
  return z

I have never needed to declare a global, it gets harder to understand what's changing your values around.

Remember also that global is only global to the module, not the whole program.

···

---
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Chris Barker wrote:

Hi all,

I'm getting the following warning when my app starts up:

(process:5199): Gdk-WARNING **: locale not supported by C library

(process:5199): Gtk-WARNING **: Locale not supported by C library.
        Using the fallback 'C' locale.

it seems to be happening in the wx.App.__incit__, before OnInit is called.

The strange part is that it doesn't happen with another my wxPython apps. The only difference I can see is that I call wx.InitAllImageHandlers in the one with the errors, bu that's in OnInit, and the errors seem to come before that.

Anyone have any ideas?

As part of the initialization of GTK gtk_set_locale() is called which initializes the i18n support in GTK and then calls the C lib's locale functions. Apparently the C lib doesn't support whatever locale is trying to be set and is returning an error which GTK is reporting. Check the values of the LANG and LC_* environment variables.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

As part of the initialization of GTK gtk_set_locale() is called which initializes the i18n support in GTK and then calls the C lib's locale functions. Apparently the C lib doesn't support whatever locale is trying to be set and is returning an error which GTK is reporting. Check the values of the LANG and LC_* environment variables.

I don't have any LANG or LC_* environment variables..maybe that's that problem? What should they be for American English?

I'm still confused, as this doesn't happen with every wxPythonGTK app, but it seems to be happening on initialization, which should be the same for all....

Not to mention other GTK and wxGTK apps...

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Chris Barker wrote:

Robin Dunn wrote:

As part of the initialization of GTK gtk_set_locale() is called which initializes the i18n support in GTK and then calls the C lib's locale functions. Apparently the C lib doesn't support whatever locale is trying to be set and is returning an error which GTK is reporting. Check the values of the LANG and LC_* environment variables.

I don't have any LANG or LC_* environment variables..maybe that's that problem? What should they be for American English?

Here is what I have, (although just LANG is probably enough since they are all the same):

LANG=en_US
LANGUAGE=en_US:en
LC_ADDRESS=en_US
LC_COLLATE=en_US
LC_CTYPE=en_US
LC_IDENTIFICATION=en_US
LC_MEASUREMENT=en_US
LC_MESSAGES=en_US
LC_MONETARY=en_US
LC_NAME=en_US
LC_NUMERIC=en_US
LC_PAPER=en_US
LC_TELEPHONE=en_US
LC_TIME=en_US

I'm still confused, as this doesn't happen with every wxPythonGTK app, but it seems to be happening on initialization, which should be the same for all....

Very weird. Are they all using the same version of wxPython?

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!