beginner self.Bind() problems

Hello,

I’m new to both Python and wxPython so I’m still struggling with some of the basic concepts. What I am trying to do is create an application where I have a check box and a text box. If the check box is checked, then the text box should be white and editable, while if the check box is unchecked, the text box should be grey and uneditable (this behavior in quite common in various programs).

I managed to create a program which has this functionality and I’m pasting the code below. However, due to my lack of experience I’m not sure whether I’ve written my code in the best possible way. In particular I have a few questions:

  1. In the functions makeselection, I have explicit reference to check_box_1 and text_box_1, which means that if I have 10 such check box / text box pairs in my final application, I need 10 makeselection functions, which seems quite inefficient. I’m guessing that there is a way to make just one function definition and then call it with various variables, however I couldn’t figure out how to do it. Or am I wrong?

  2. In the function definition makeselection(self,event), what does ‘event’ stand for, since it is not explicitly given in the self.makeselection call? Is ‘event’ = wx.EVT_CHECKBOX? However if I call self.makeselection(wx.EVT_CHECKBOX), then the program doesn’t work as desired any more. Alternatively if I call self.makeselection(), then I get an error.

  3. What is the difference between self. and ? I’m guessing works just withing the method definition, but if this variable needs to be used in a different method in the same class it needs to be called self..

  4. The last one is purely a cosmetic one. If you notice the label and text box are not aligned properly, although the have the same y position coordinate. Is there a function to align them automatically, or should I just fiddle with the y positions?

Thanks a lot in advance to whoever might have some time to help me out!

Jopeto

Here’s the code:

#!/usr/bin/env python

import wx

class exampleclass(wx.Frame):
def init(self,parent,id):
wx.Frame.init(self,parent,id,title=‘Main window’, pos=(200,100), size=(500,500))
mainpanel=wx.Panel(self)

  self.check_box_1 = wx.CheckBox(parent=mainpanel, id=-1, label='Enable box', pos=(100,100), size=wx.DefaultSize)
  self.check_box_1.SetValue(True)

  self.static_label_1 = wx.StaticText(parent=mainpanel, id=-1, label='Label', pos=(100,150), size=wx.DefaultSize, style=wx.ALIGN_LEFT)
  self.text_box_1 = wx.TextCtrl(parent=mainpanel, id=-1, value='example text', pos=(200,150), size=(100,-1))

  self.Bind(wx.EVT_CHECKBOX, self.makeselection, self.check_box_1)    

def makeselection(self,event):
if self.check_box_1.IsChecked()==False:
self.text_box_1.SetEditable(False)
self.text_box_1.SetBackgroundColour(‘grey’)
else:
self.text_box_1.SetEditable(True)
self.text_box_1.SetBackgroundColour(‘white’)

if name==‘main’:
app=wx.PySimpleApp()
mainframe=exampleclass(parent=None,id=-1)
mainframe.Show()
app.MainLoop()

Hello,

I’m new to both Python and wxPython so I’m still struggling with some of the basic concepts. What I am trying to do is create an application where I have a check box and a text box. If the check box is checked, then the text box should be white and editable, while if the check box is unchecked, the text box should be grey and uneditable (this behavior in quite common in various programs).

I managed to create a program which has this functionality and I’m pasting the code below. However, due to my lack of experience I’m not sure whether I’ve written my code in the best possible way. In particular I have a few questions:

  1. In the functions makeselection, I have explicit reference to check_box_1 and text_box_1, which means that if I have 10 such check box / text box pairs in my final application, I need 10 makeselection functions, which seems quite inefficient. I’m guessing that there is a way to make just one function definition and then call it with various variables, however I couldn’t figure out how to do it. Or am I wrong?

Welcome to the wonderful and wacky world of wxPython!

You can bind multiple widgets to the same event handler. That would be one way to do it. There’s a tutorial on the process here: http://www.blog.pythonlibrary.org/2011/09/20/wxpython-binding-multiple-widgets-to-the-same-handler/

I would create some kind of widget dictionary, like this:

self.widgetDict = {“checkboxOne” : self.textOne, “checkboxTwo” : self.textTwo}

Then in the event handler, you’d just do something like

checkbox = event.GetEventObject()

if checkbox.IsChecked():

self.widgetDict[checkbox.GetName()].Enable()

else:

self.widgetDict[checkbox.GetName()].Disable()

I hope that makes sense…

  1. In the function definition makeselection(self,event), what does ‘event’ stand for, since it is not explicitly given in the self.makeselection call? Is ‘event’ = wx.EVT_CHECKBOX? However if I call self.makeselection(wx.EVT_CHECKBOX), then the program doesn’t work as desired any more. Alternatively if I call self.makeselection(), then I get an error.

If you add a “print type(event)” you’ll find out what event that is. The event is sent implicitly when the widget is checked. I think there’s a lot of other information included, but I don’t know how to describe it. But when you check the checkbox, it sends both the “self” and the “event” arguments whereas “self.makeselection()” only passes “self”.

  1. What is the difference between self. and ? I’m guessing works just withing the method definition, but if this variable needs to be used in a different method in the same class it needs to be called self..

The “self” that is prepended on variable names makes the variable accessible to the rest of the class, as you’ve noted. I’ve heard lots of different terms for those things and it seems to depend on who is writing the book. Some call it an “instance attribute” (http://stackoverflow.com/questions/2709821/python-self-explained) while others might call it a class variable or who knows what. Without “self” though, it becomes a local variable and goes out of scope as soon as the method or function ends.

  1. The last one is purely a cosmetic one. If you notice the label and text box are not aligned properly, although the have the same y position coordinate. Is there a function to align them automatically, or should I just fiddle with the y positions?

The label DOES match up with the textbox. Notice that both the label’s top left corner and the text control’s are lined up. To fix that, you could use sizers, change the label size or just set the position slightly lower as you mentioned.

Thanks a lot in advance to whoever might have some time to help me out!

Jopeto

I hope that helps a bit. I’m sure others will jump in with their own ideas and suggestions.

···

On Tue, Oct 4, 2011 at 4:30 AM, G. Nikiforov g_nikiforov@hotmail.com wrote:


Mike Driscoll

Blog: http://blog.pythonlibrary.org

Just wondering would disabling the TextCtrl be more suitable?

   def makeselection(self,event):
      if self.check_box_1.IsChecked()==False:
        self.text_box_1.Disable()
      else:
        self.text_box_1.Enable()

Just a quick thought on an alternative to Mike's suggestion for this one. You could make a class that creates and manages one text/checkbox pair. Then it can handle the events for just its own pair of widgets and call methods or pass messages to the other parts of the application that need to deal with the results of the events if needed

···

On 10/4/11 2:30 AM, G. Nikiforov wrote:

Hello,

I'm new to both Python and wxPython so I'm still struggling with some of
the basic concepts. What I am trying to do is create an application
where I have a check box and a text box. If the check box is checked,
then the text box should be white and editable, while if the check box
is unchecked, the text box should be grey and uneditable (this behavior
in quite common in various programs).

I managed to create a program which has this functionality and I'm
pasting the code below. However, due to my lack of experience I'm not
sure whether I've written my code in the best possible way. In
particular I have a few questions:

1. In the functions makeselection, I have explicit reference to
check_box_1 and text_box_1, which means that if I have 10 such check box
/ text box pairs in my final application, I need 10 makeselection
functions, which seems quite inefficient. I'm guessing that there is a
way to make just one function definition and then call it with various
variables, however I couldn't figure out how to do it. Or am I wrong?

--
Robin Dunn
Software Craftsman

I like Robin’s idea too, although for a new Python guy, it might be a little hard to figure out.

···

Mike Driscoll

Blog: http://blog.pythonlibrary.org

Dear Mike, Paul and Robin,

Thanks a lot for your quick replies! They were extremely helpful. I guess I’m still struggling a bit with the terminology which is used in the online manuals and tutorials so I hit dead ends, but your replies cleared up a few things.

Thanks once again,
Jopeto

···

From: g_nikiforov@hotmail.com
To: wxpython-users@googlegroups.com
Subject: [wxPython-users] beginner self.Bind() problems
Date: Tue, 4 Oct 2011 12:30:34 +0300

Hello,

I’m new to both Python and wxPython so I’m still struggling with some of the basic concepts. What I am trying to do is create an application where I have a check box and a text box. If the check box is checked, then the text box should be white and editable, while if the check box is unchecked, the text box should be grey and uneditable (this behavior in quite common in various programs).

I managed to create a program which has this functionality and I’m pasting the code below. However, due to my lack of experience I’m not sure whether I’ve written my code in the best possible way. In particular I have a few questions:

  1. In the functions makeselection, I have explicit reference to check_box_1 and text_box_1, which means that if I have 10 such check box / text box pairs in my final application, I need 10 makeselection functions, which seems quite inefficient. I’m guessing that there is a way to make just one function definition and then call it with various variables, however I couldn’t figure out how to do it. Or am I wrong?

  2. In the function definition makeselection(self,event), what does ‘event’ stand for, since it is not explicitly given in the self.makeselection call? Is ‘event’ = wx.EVT_CHECKBOX? However if I call self.makeselection(wx.EVT_CHECKBOX), then the program doesn’t work as desired any more. Alternatively if I call self.makeselection(), then I get an error.

  3. What is the difference between self. and ? I’m guessing works just withing the method definition, but if this variable needs to be used in a different method in the same class it needs to be called self..

  4. The last one is purely a cosmetic one. If you notice the label and text box are not aligned properly, although the have the same y position coordinate. Is there a function to align them automatically, or should I just fiddle with the y positions?

Thanks a lot in advance to whoever might have some time to help me out!

Jopeto

Here’s the code:

#!/usr/bin/env python

import wx

class exampleclass(wx.Frame):
def init(self,parent,id):
wx.Frame.init(self,parent,id,title=‘Main window’, pos=(200,100), size=(500,500))
mainpanel=wx.Panel(self)

  self.check_box_1 = wx.CheckBox(parent=mainpanel, id=-1, label='Enable box', pos=(100,100), size=wx.DefaultSize)
  self.check_box_1.SetValue(True)

  self.static_label_1 = wx.StaticText(parent=mainpanel, id=-1, label='Label', pos=(100,150), size=wx.DefaultSize, style=wx.ALIGN_LEFT)
  self.text_box_1 = wx.TextCtrl(parent=mainpanel, id=-1, value='example text', pos=(200,150), size=(100,-1))

  self.Bind(wx.EVT_CHECKBOX, self.makeselection, self.check_box_1)    

def makeselection(self,event):
if self.check_box_1.IsChecked()==False:
self.text_box_1.SetEditable(False)
self.text_box_1.SetBackgroundColour(‘grey’)
else:
self.text_box_1.SetEditable(True)
self.text_box_1.SetBackgroundColour(‘white’)

if name==‘main’:
app=wx.PySimpleApp()
mainframe=exampleclass(parent=None,id=-1)
mainframe.Show()
app.MainLoop()

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en