简体   繁体   English

在wxPython中使用参数调用事件

[英]Calling event with argument in wxPython

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. 我正在用wx Python编写一个应用程序,该应用程序应该获取一些值,输入到TextCtrl-boxes中,然后在按下开始按钮时将其传递给函数。 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. 如您所见,OnQuitApp()不需要调用任何参数,甚至不需要括号。 OnStart() on the other hand, needs arguments. 另一方面,OnStart()需要参数。 The ones I'd like to include are DispPort and GpsPort. 我想包括的是DispPort和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'. 第一个有两个参数,第二个获取的参数类型错误,第三个使用未定义的变量'event'。

Where do I go from here? 我从这里去哪里?

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 ! 当事件被触发时(通过单击), wxPython使用参数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 . 您可以使用lambda: … ,有关详细信息,请参见Link But this should normally not be necessary, use something like pubsub if your demands for event passing are not covered by wxWidgets. 但这通常不是必须的,如果wxWidgets不满足您对事件传递的要求,请使用pubsub东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM