0

I'm writing an application in wx Python that is supposed to get some values, entered into TextCtrl-boxes, and passing them on to a function as you press a start button. This has proven complicated.

This is the script:

class Example(wx.Frame):

    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title,
            size=(300, 200))

        self.InitUI()
        self.Centre()
        self.Show()

    def InitUI(self):

        panel = wx.Panel(self)

        sizer = wx.GridBagSizer(4, 2)

        text1 = wx.StaticText(panel, label="Set COM-ports")
        sizer.Add(text1, pos=(0, 0), flag=wx.TOP|wx.LEFT|wx.BOTTOM,
            border=15)

        line = wx.StaticLine(panel)
        sizer.Add(line, pos=(1, 0), span=(1, 5),
            flag=wx.EXPAND|wx.BOTTOM, border=10)

        text2 = wx.StaticText(panel, label="Dispersion control port:")
        sizer.Add(text2, pos=(2, 0), flag=wx.ALIGN_RIGHT|wx.LEFT, border=10)

        tc1 = wx.TextCtrl(panel)
        sizer.Add(tc1, pos=(2, 1), flag=wx.LEFT, border=10)

        text3 = wx.StaticText(panel, label="GPS port:")
        sizer.Add(text3, pos=(3, 0),flag=wx.ALIGN_RIGHT|wx.LEFT, border=10)

        tc2 = wx.TextCtrl(panel)
        sizer.Add(tc2, pos=(3, 1), flag=wx.LEFT,border=10)

        button4 = wx.Button(panel, label="Start")
        DispPort=tc1.GetValue()
        GpsPort=tc2.GetValue()
        sizer.Add(button4, pos=(4, 0), flag=wx.ALIGN_RIGHT)
        button4.Bind(wx.EVT_BUTTON, self.OnStart(self, event,DispPort,GpsPort))

        button5 = wx.Button(panel, wx.ID_EXIT, label="Cancel")
        sizer.Add(button5, pos=(4, 1), flag=wx.ALIGN_LEFT|wx.LEFT, border=10)
        self.Bind(wx.EVT_BUTTON, self.OnQuitApp, id=wx.ID_EXIT)

        panel.SetSizer(sizer)

    def OnStart(self, event, DispPort, GpsPort):
        stdial=Startclass(self,DispPort,GpsPort)

    def OnQuitApp(self, event):

        self.Close()

As you can see OnQuitApp() needs no arguments, not even parentheses, to be called. OnStart() on the other hand, needs arguments. The ones I'd like to include are DispPort and GpsPort. But writing

self.OnStart(DispPort,GpsPort)

does not work. Writing

self.OnStart(self, DispPort,GpsPort)

does not work either. Neither does

self.OnStart(self,event,DispPort,GpsPort)

The first has two few arguments, the second gets the arguments to be of the wrong type, the third uses the undefined variable 'event'.

Where do I go from here?

1 Answer 1

2

Short Answer: You may have got wrong how to deal with events. When binding you pass a function/method reference (and not calling it already as you did)! When the event gets triggered (by clicking), wxPython calls the event handler with the argument event! In your case you would write:

    self.tc1 = tc1
    # bind buton click to event handler. You pass the function!
    button4.Bind(wx.EVT_BUTTON, self.OnStart)

    …
# the event handler. The parameter ``event`` is mandatory! 
def onStart(self, event):
    DispPort = self.tc1.GetValue()
    GpsPort = self.tc2.GetValue()
    stdial = Startclass(self, DispPort, GpsPort)

Long Answer: Yo can actually pass arguments to the event handler, but this is not necessary in that simple case. You can do it by using lambda: …, for details see Link. But this should normally not be necessary, use something like pubsub if your demands for event passing are not covered by wxWidgets.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.