Forcing OnClick on wx.Panel.

Dear all,

I am developing a WX interface where the user can click on buttons,
and after click I run some steps I defined.
A sample code I grab from http://www.wellho.net/resources/ex.php4?item=y207/wx04.py
is shown below.

But now I need to force that on the starting up a not pressed button
is assumed as pressed.
On the sample below I would like that if I run:
  python mycode.py

automatically OnClick with GetId()==10 is assumed.

Any help are welcome.

Thanks

milton

···

===

import wx

# Set up a panel within a frame with a load of widgets
# Set up events on each of the widgets
# display the results in a scrolling text frame

class Form1(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        # Set up some basic element. Placement is a bit crude as this
        # is really an event handling demo! It would also be a good
idea
        # to define constants for the IDs!

        self.quote = wx.StaticText(self, -1, "Your quote :",wx.Point
(20, 30))
        self.logger = wx.TextCtrl(self,5, "",wx.Point(330,20), wx.Size
(200,300), \
                wx.TE_MULTILINE | wx.TE_READONLY)
        self.button = wx.Button(self, 10, "Save", wx.Point(200, 325))
        self.leaver = wx.Button(self, 11, "Quit", wx.Point(200, 355))
        self.lblname = wx.StaticText(self, -1, "Your name :",wx.Point
(20,60))
        self.editname = wx.TextCtrl(self, 20, "Enter here your name",
wx.Point(170, 60), wx.Size(140,-1))
        self.lblhear = wx.StaticText(self,-1,"How did you hear about
us ?",wx.Point(20, 90))
        self.sampleList = ['friends', 'colleagues', 'advertising',
'web search']
        self.edithear=wx.ComboBox(self, 30, "", wx.Point(170, 90),
wx.Size(95, -1),
                   self.sampleList, wx.CB_DROPDOWN)
        self.insure = wx.CheckBox(self, 40, "Do you want Insured
Shipment ?",wx.Point(20,180))
        self.radioList = ['blue', 'red', 'yellow', 'orange', 'green',
'magenta',
                      'black', 'grey']
        rb = wx.RadioBox(self, 50, "What colour would you like ?",
wx.Point(20, 210), wx.DefaultSize,
                        self.radioList, 4, wx.RA_SPECIFY_COLS)

        # Add event handlers as appropriate
        # Two handlers on id #30 as it's a select box you can also
type into

        wx.EVT_BUTTON(self, 10, self.OnClick)
        wx.EVT_BUTTON(self, 11, self.OnClick) # Share handler between
several ids
        wx.EVT_BUTTON(self, 12, self.OnClick) # Share handler between
several ids

        wx.EVT_TEXT(self, 20, self.EvtText)
        wx.EVT_CHAR(self.editname, self.EvtChar) # on id 20
        wx.EVT_COMBOBOX(self, 30, self.EvtComboBox)
        wx.EVT_TEXT(self, 30, self.EvtText)
        wx.EVT_CHECKBOX(self, 40, self.EvtCheckBox)
        wx.EVT_RADIOBOX(self, 50, self.EvtRadioBox)

        # parent is the top level frame - we can title a frame but
        # not a panel!
        parent.SetTitle("Quotation Request")
        # Absolute placement as we've done above is NOT a good idea,
but it
        # does mean that this demo lets you see the events more
easily. What
        # you should do is use a sizer, and then set the parent's size
with
        # the following (or the equivalent)
        # parent.SetSizerAndFit(sizer)

    def EvtRadioBox(self, event):
        self.logger.AppendText('EvtRadioBox: %d\n' % event.GetInt())

    def EvtComboBox(self, event):
        self.logger.AppendText('EvtComboBox: %s\n' % event.GetString
())

    def OnClick(self,event):
        self.logger.AppendText(" Click on object with Id %d\n"
%event.GetId())
        if event.GetId() == 11:
                frame.Destroy()
        if event.GetId() == 12:
                self.logger.AppendText(" Starting with GetId()
12")

    def EvtText(self, event):
        self.logger.AppendText('EvtText: %s\n' % event.GetString())

    def EvtChar(self, event):
        self.logger.AppendText('EvtChar: %d\n' % event.GetKeyCode())
        event.Skip()

    def EvtCheckBox(self, event):
        self.logger.AppendText('EvtCheckBox: %d\n' % event.Checked())

app = wx.PySimpleApp()
frame = wx.Frame(None, size=(550,425)) # top level window
# We have only had to set the size because the simple layout used
# hasn't allowed for an automatic sizing.
Form1(frame) # A panel in the frame
frame.Show(1)
app.MainLoop()

So in other words, you are wanting to do the same thing that would be done if a button is clicked, but to do it programatically without an event from the user. Right? If so then the typical way that is done is to refactor and split out the functional code into another method. For example, if you currently have this:

  def OnDoSomething(self, evt):
    foo()
    bar()
    ham()
    eggs()

then change it to this:

  def OnDoSomething(self, evt):
    self.Something()

  def Something(self):
    foo()
    bar()
    ham()
    eggs()

And then call Something() from wherever you need to do it.

···

miltinho.astronauta@gmail.com wrote:

Dear all,

I am developing a WX interface where the user can click on buttons,
and after click I run some steps I defined.
A sample code I grab from Panel and event handler - Python example
is shown below.

But now I need to force that on the starting up a not pressed button
is assumed as pressed.
On the sample below I would like that if I run:
  python mycode.py

automatically OnClick with GetId()==10 is assumed.

--
Robin Dunn
Software Craftsman

Hi Robin Dunn,

Thank you very much for the help.

I will try manage this usint your suggestions.

cheers

milton

···

2009/9/7 Robin Dunn robin@alldunn.com

miltinho.astronauta@gmail.com wrote:

Dear all,

I am developing a WX interface where the user can click on buttons,
and after click I run some steps I defined.

A sample code I grab from http://www.wellho.net/resources/ex.php4?item=y207/wx04.py
is shown below.

But now I need to force that on the starting up a not pressed button

is assumed as pressed.
On the sample below I would like that if I run:
python mycode.py

automatically OnClick with GetId()==10 is assumed.

So in other words, you are wanting to do the same thing that would be

done if a button is clicked, but to do it programatically without an
event from the user. Right? If so then the typical way that is done is
to refactor and split out the functional code into another method. For

example, if you currently have this:

   def OnDoSomething(self, evt):
           foo()
           bar()
           ham()
           eggs()

then change it to this:

   def OnDoSomething(self, evt):

           self.Something()

   def Something(self):
           foo()
           bar()
           ham()
           eggs()

And then call Something() from wherever you need to do it.


Robin Dunn
Software Craftsman
http://wxPython.org