How to reference a button...

Hi there,
I’m creating buttons with a code snippet from “wxPython In Action”, page 124:
def buildOneButton(self. parent, …):

it works great!
my problem now is how to reference the button object to for example change the label.

Thanks in advance.

Hello Jorge,

Please, see if this little example (code in attachment) can help you.

Regards, Rene

button.pyw (2.52 KB)

···

On Tue, Apr 14, 2009 at 4:26 PM, Jorge starglider101@gmail.com wrote:

Hi there,
I’m creating buttons with a code snippet from “wxPython In Action”, page 124:

def buildOneButton(self. parent, …):

it works great!
my problem now is how to reference the button object to for example change the label.

Thanks in advance.


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Hi Rene,
your example use the panel id to find the button.
My problem is different I have one wx.Frame (no panel) with a lot of buttons and when one button is
pressed I know witch was by the index, but I need to change the color of the button and
don’t know how to reference it.

Attached goes a files with my code.

Thank for your help.

Regards,

Jorge

btns_m.py (2.54 KB)

···

On Tue, Apr 14, 2009 at 3:49 PM, Rene Heymans rene.heymans@gmail.com wrote:

Hello Jorge,

Please, see if this little example (code in attachment) can help you.

Regards, Rene

On Tue, Apr 14, 2009 at 4:26 PM, Jorge starglider101@gmail.com wrote:

Hi there,
I’m creating buttons with a code snippet from “wxPython In Action”, page 124:

def buildOneButton(self. parent, …):

it works great!
my problem now is how to reference the button object to for example change the label.

Thanks in advance.


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Hi,

···

On Tue, Apr 14, 2009 at 4:30 PM, Jorge wrote:

Hi Rene,
your example use the panel id to find the button.
My problem is different I have one wx.Frame (no panel) with a lot of buttons
and when one button is
pressed I know witch was by the index, but I need to change the color of the
button and
don't know how to reference it.

Just put a line like this:

button = event.GetEventObject()

in your OnSetRank method and you'll get the reference to the button.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

Hello,

Hi Rene,
your example use the panel id to find the button.
My problem is different I have one wx.Frame (no panel) with a lot of buttons and when one button is
pressed I know witch was by the index, but I need to change the color of the button and

don’t know how to reference it.

Attached goes a files with my code.

Thank for your help.

There are a number of ways to do this here a couple,

  1. Save a reference to the button when you create it

i.e) self._some_buttton = wx.Button(…)

  1. Use the event object in your handler

button = evt.GetEventObject()
button.SomeButtonMethod(…)

Cody

···

On Tue, Apr 14, 2009 at 10:30 AM, Jorge starglider101@gmail.com wrote:

Well one problem is solved!
But I need to change the labels for example just in some
buttons due to update, without any event!
How do I update a button without a event?

Thank you.

···

On Tue, Apr 14, 2009 at 4:37 PM, Cody Precord codyprecord@gmail.com wrote:

Hello,

On Tue, Apr 14, 2009 at 10:30 AM, Jorge starglider101@gmail.com wrote:

Hi Rene,
your example use the panel id to find the button.
My problem is different I have one wx.Frame (no panel) with a lot of buttons and when one button is
pressed I know witch was by the index, but I need to change the color of the button and

don’t know how to reference it.

Attached goes a files with my code.

Thank for your help.

There are a number of ways to do this here a couple,

  1. Save a reference to the button when you create it

i.e) self._some_buttton = wx.Button(…)

  1. Use the event object in your handler

button = evt.GetEventObject()
button.SomeButtonMethod(…)

Cody


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Hello Jorge,

Andrea and Cody already gave you answers. I can’t tell more or better (I’m a newbie in programming, Python and wxPython).

Just one remark: I don’t understand why you say “no panel”. Actually you do create one in your init function and you pass the panel as an argument of the mk_btn function, which in turn calls the buildOneButton function where the parent is the panel. My understanding of a panel is that it serves as “a container to contain controls”. It seems to be a good idea to always have a panel to put buttons on it. I tried once and it behaves differently. For instance, when you create a button with a frame (wx.Frame) as a parent, the button occupies immediately the whole frame surface.

Good work,

Rene

···

On Tue, Apr 14, 2009 at 5:30 PM, Jorge starglider101@gmail.com wrote:

Hi Rene,
your example use the panel id to find the button.
My problem is different I have one wx.Frame (no panel) with a lot of buttons and when one button is

pressed I know witch was by the index, but I need to change the color of the button and
don’t know how to reference it.

Attached goes a files with my code.

Thank for your help.

Regards,

Jorge

On Tue, Apr 14, 2009 at 3:49 PM, Rene Heymans rene.heymans@gmail.com wrote:

Hello Jorge,

Please, see if this little example (code in attachment) can help you.

Regards, Rene

On Tue, Apr 14, 2009 at 4:26 PM, Jorge starglider101@gmail.com wrote:

Hi there,
I’m creating buttons with a code snippet from “wxPython In Action”, page 124:
def buildOneButton(self. parent, …):

it works great!
my problem now is how to reference the button object to for example change the label.

Thanks in advance.


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Hell Jorge,

Buttons, like any widget, do have an id (either by default when id=-1 or one set with id=wx.NewID() or by explicit assignment id=123) but I understand they also have a name (a feature I have never tried). When you create the button, you could try giving the button the same name as its label (in your case str(cnt)) and later use the name to identify the specific button you wish.

I hope what I say makes sense. If not you must wait for a suggestion from experts.

Cheers, Rene

···

On Tue, Apr 14, 2009 at 6:09 PM, Jorge starglider101@gmail.com wrote:

Well one problem is solved!
But I need to change the labels for example just in some
buttons due to update, without any event!

How do I update a button without a event?

Thank you.

On Tue, Apr 14, 2009 at 4:37 PM, Cody Precord codyprecord@gmail.com wrote:

Hello,

On Tue, Apr 14, 2009 at 10:30 AM, Jorge starglider101@gmail.com wrote:

Hi Rene,
your example use the panel id to find the button.
My problem is different I have one wx.Frame (no panel) with a lot of buttons and when one button is

pressed I know witch was by the index, but I need to change the color of the button and
don’t know how to reference it.

Attached goes a files with my code.

Thank for your help.

There are a number of ways to do this here a couple,

  1. Save a reference to the button when you create it

i.e) self._some_buttton = wx.Button(…)

  1. Use the event object in your handler

button = evt.GetEventObject()
button.SomeButtonMethod(…)

Cody


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Rene,

Hello Jorge,
Andrea and Cody already gave you answers. I can't tell more or better (I'm a newbie in programming, Python and wxPython).
Just one remark: I don't understand why you say "no panel". Actually you do create one in your __init__ function and you pass the panel as an argument of the mk_btn function, which in turn calls the buildOneButton function where the parent is the panel. My understanding of a panel is that it serves as "a container to contain controls". It seems to be a good idea to always have a panel to put buttons on it. I tried once and it behaves differently. For instance, when you create a button with a frame (wx.Frame) as a parent, the button occupies immediately the whole frame surface.
Good work,
Rene

I think there's some old examples out there that still use the frame as a parent rather than a panel. The frame will not look native on Windows though, so it is recommended practice to use a panel for the widget container as the panel object looks right on all platforms.

If Jorge set the size of the buttons or just has multiple ones, then they shouldn't take up all the space.

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Jorge wrote:

Well one problem is solved!
But I need to change the labels for example just in some
buttons due to update, without any event!
How do I update a button without a event?

Thank you.

I must be missing something here. How do you know when a button needs its label changed if there is no event to let the GUI know that? Or are you polling for data and you want the buttons labels to change depending on what data is being returned? Either way, it's pretty easy:

def Update(self):
     self.buttonOne.SetLabel("some label")
     self.buttonTwo.SetLabel("another label")

Of course, you may want to enclose the SetLabel calls in an "if" statement since it sounds like you want to conditionally change the button labels based on some nebulous concept that you haven't explained.

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Thank you Mike for your comment.

Regards, Rene

···

On Tue, Apr 14, 2009 at 7:33 PM, Mike Driscoll mike@pythonlibrary.org wrote:

Rene,

Hello Jorge,
Andrea and Cody already gave you answers. I can’t tell more or better (I’m a newbie in programming, Python and wxPython).

Just one remark: I don’t understand why you say “no panel”. Actually you do create one in your init function and you pass the panel as an argument of the mk_btn function, which in turn calls the buildOneButton function where the parent is the panel. My understanding of a panel is that it serves as “a container to contain controls”. It seems to be a good idea to always have a panel to put buttons on it. I tried once and it behaves differently. For instance, when you create a button with a frame (wx.Frame) as a parent, the button occupies immediately the whole frame surface.

Good work,
Rene

I think there’s some old examples out there that still use the frame as a parent rather than a panel. The frame will not look native on Windows though, so it is recommended practice to use a panel for the widget container as the panel object looks right on all platforms.

If Jorge set the size of the buttons or just has multiple ones, then they shouldn’t take up all the space.


Mike Driscoll

Blog: http://blog.pythonlibrary.org


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Jorge, please accept my apologies.

I guess you understood I was writing “Hello Jorge”.

I’m very embarrassed by making such stupid typo. I just see it now.

Thank you, Rene

···

On Tue, Apr 14, 2009 at 6:24 PM, Rene Heymans rene.heymans@gmail.com wrote:

Hell Jorge,

Buttons, like any widget, do have an id (either by default when id=-1 or one set with id=wx.NewID() or by explicit assignment id=123) but I understand they also have a name (a feature I have never tried). When you create the button, you could try giving the button the same name as its label (in your case str(cnt)) and later use the name to identify the specific button you wish.

I hope what I say makes sense. If not you must wait for a suggestion from experts.

Cheers, Rene

On Tue, Apr 14, 2009 at 6:09 PM, Jorge starglider101@gmail.com wrote:

Well one problem is solved!
But I need to change the labels for example just in some
buttons due to update, without any event!

How do I update a button without a event?

Thank you.

On Tue, Apr 14, 2009 at 4:37 PM, Cody Precord codyprecord@gmail.com wrote:

Hello,

On Tue, Apr 14, 2009 at 10:30 AM, Jorge starglider101@gmail.com wrote:

Hi Rene,
your example use the panel id to find the button.
My problem is different I have one wx.Frame (no panel) with a lot of buttons and when one button is

pressed I know witch was by the index, but I need to change the color of the button and
don’t know how to reference it.

Attached goes a files with my code.

Thank for your help.

There are a number of ways to do this here a couple,

  1. Save a reference to the button when you create it

i.e) self._some_buttton = wx.Button(…)

  1. Use the event object in your handler

button = evt.GetEventObject()
button.SomeButtonMethod(…)

Cody


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Rene,
I made confusion with other test I’m doing.
Forget the panel, this one as panel.

Thank you.

···

On Tue, Apr 14, 2009 at 5:24 PM, Rene Heymans rene.heymans@gmail.com wrote:

Hell Jorge,

Buttons, like any widget, do have an id (either by default when id=-1 or one set with id=wx.NewID() or by explicit assignment id=123) but I understand they also have a name (a feature I have never tried). When you create the button, you could try giving the button the same name as its label (in your case str(cnt)) and later use the name to identify the specific button you wish.

I hope what I say makes sense. If not you must wait for a suggestion from experts.

Cheers, Rene

On Tue, Apr 14, 2009 at 6:09 PM, Jorge starglider101@gmail.com wrote:

Well one problem is solved!
But I need to change the labels for example just in some

buttons due to update, without any event!

How do I update a button without a event?

Thank you.

On Tue, Apr 14, 2009 at 4:37 PM, Cody Precord codyprecord@gmail.com wrote:

Hello,

On Tue, Apr 14, 2009 at 10:30 AM, Jorge starglider101@gmail.com wrote:

Hi Rene,
your example use the panel id to find the button.
My problem is different I have one wx.Frame (no panel) with a lot of buttons and when one button is

pressed I know witch was by the index, but I need to change the color of the button and
don’t know how to reference it.

Attached goes a files with my code.

Thank for your help.

There are a number of ways to do this here a couple,

  1. Save a reference to the button when you create it

i.e) self._some_buttton = wx.Button(…)

  1. Use the event object in your handler

button = evt.GetEventObject()
button.SomeButtonMethod(…)

Cody


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Hi Mike,
you are right about the panel, but it’s from another test I’m doing.

I partial solve my main problem:
to update the label buttons r according to what is in a database.
How?
When the buttons are created I make a copy of the object to a list then I recall the list
and update the button.
Your method, Mike is easy but I have 400 buttons to process!

Thank you.

···

On Tue, Apr 14, 2009 at 6:36 PM, Mike Driscoll mike@pythonlibrary.org wrote:

Jorge wrote:

Well one problem is solved!

But I need to change the labels for example just in some

buttons due to update, without any event!

How do I update a button without a event?

Thank you.

I must be missing something here. How do you know when a button needs its label changed if there is no event to let the GUI know that? Or are you polling for data and you want the buttons labels to change depending on what data is being returned? Either way, it’s pretty easy:

def Update(self):

self.buttonOne.SetLabel("some label")

self.buttonTwo.SetLabel("another label")

Of course, you may want to enclose the SetLabel calls in an “if” statement since it sounds like you want to conditionally change the button labels based on some nebulous concept that you haven’t explained.


Mike Driscoll

Blog: http://blog.pythonlibrary.org


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Rene,
I did not notice. R.O.F.L. 360.gif
No problem.

···

On Tue, Apr 14, 2009 at 6:50 PM, Rene Heymans rene.heymans@gmail.com wrote:

Jorge, please accept my apologies.

I guess you understood I was writing “Hello Jorge”.

I’m very embarrassed by making such stupid typo. I just see it now.

Thank you, Rene

On Tue, Apr 14, 2009 at 6:24 PM, Rene Heymans rene.heymans@gmail.com wrote:

Hell Jorge,

Buttons, like any widget, do have an id (either by default when id=-1 or one set with id=wx.NewID() or by explicit assignment id=123) but I understand they also have a name (a feature I have never tried). When you create the button, you could try giving the button the same name as its label (in your case str(cnt)) and later use the name to identify the specific button you wish.

I hope what I say makes sense. If not you must wait for a suggestion from experts.

Cheers, Rene

On Tue, Apr 14, 2009 at 6:09 PM, Jorge starglider101@gmail.com wrote:

Well one problem is solved!
But I need to change the labels for example just in some

buttons due to update, without any event!

How do I update a button without a event?

Thank you.

On Tue, Apr 14, 2009 at 4:37 PM, Cody Precord codyprecord@gmail.com wrote:

Hello,

On Tue, Apr 14, 2009 at 10:30 AM, Jorge starglider101@gmail.com wrote:

Hi Rene,
your example use the panel id to find the button.
My problem is different I have one wx.Frame (no panel) with a lot of buttons and when one button is

pressed I know witch was by the index, but I need to change the color of the button and
don’t know how to reference it.

Attached goes a files with my code.

Thank for your help.

There are a number of ways to do this here a couple,

  1. Save a reference to the button when you create it

i.e) self._some_buttton = wx.Button(…)

  1. Use the event object in your handler

button = evt.GetEventObject()
button.SomeButtonMethod(…)

Cody


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Jorge wrote:

Hi Mike,
you are right about the panel, but it's from another test I'm doing.

I partial solve my main problem:
to update the label buttons r according to what is in a database.
How?
When the buttons are created I make a copy of the object to a list then I recall the list
and update the button.
Your method, Mike is easy but I have 400 buttons to process!

Give yourself what I call an "access path". In other words create some way to keep track of the buttons when you create them and then use that thing to access them later. What that access path will be can depend on your situation. For example, if they all have a unique name (or you can give them one) and you are able to know or calculate that name later when you want to change the label, then something based on the name is called for. (Two examples of that are a simple Python dictionary, or using the widget name attribute and thePanel.FindWindowByName(name))

···

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

Jorge wrote:

Hi Mike,
you are right about the panel, but it's from another test I'm doing.

I partial solve my main problem:
to update the label buttons r according to what is in a database.
How?
When the buttons are created I make a copy of the object to a list then I recall the list
and update the button.
Your method, Mike is easy but I have 400 buttons to process!

Thank you.

Maybe you're already doing this, but if you keep a list of your buttons, you can do something like this:

myBtnLst = [self.btnOne, self.btnTwo, self.btnThree]
for button in myBtnLst:
      button.SetLabel("new label")

Or you can do it generically using some code like this:

children = panel.GetChildren()
for child in children:
    if isinstance(child, wx.Button):
        child.SetLabel("new label")

Of course, you'll also need to keep track of where you are in your list of data as well, probably by using a counter of some sort.

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Thanks a lot Robin and Mike,
I thing I did what you suggest: keep track of the buttons in a list, and it works fine,
now I have another problem and made another post to the list.

Thanks to all of you.

···

On Tue, Apr 14, 2009 at 7:49 PM, Mike Driscoll mike@pythonlibrary.org wrote:

Jorge wrote:

Hi Mike,

you are right about the panel, but it’s from another test I’m doing.

I partial solve my main problem:

to update the label buttons r according to what is in a database.

How?

When the buttons are created I make a copy of the object to a list then I recall the list

and update the button.

Your method, Mike is easy but I have 400 buttons to process!

Thank you.

Maybe you’re already doing this, but if you keep a list of your buttons, you can do something like this:

myBtnLst = [self.btnOne, self.btnTwo, self.btnThree]

for button in myBtnLst:

 button.SetLabel("new label")

Or you can do it generically using some code like this:

children = panel.GetChildren()

for child in children:

if isinstance(child, wx.Button):

   child.SetLabel("new label")

Of course, you’ll also need to keep track of where you are in your list of data as well, probably by using a counter of some sort.


Mike Driscoll

Blog: http://blog.pythonlibrary.org


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users