Passing parameters to the Wxpython event handler function

Source: Internet
Author: User
Tags gtk wxwidgets

Passing parameters to the Wxpython event handler 2014-04-30 07:47:08segmentfault-Blog--Hits: 39

The recent tinkering with Python also naturally wxpython. I've used Qt and GTK + to write the GUI, but almost all of the Python books tell me that it's best to use Wxpython, and I didn't try PyQt and pygtk to start studying Wxpython. I prefer QT between Qt and GTK + because it's done well across platforms, and it's also good for commercialization. But the feeling of wxwidget is completely different.

If Wxwidgets has any advantage over QT, it's probably only a small one. From the perspective of development efficiency, if not the Python combination is really not as good as QT; from the perspective of business support wxwidgets there is no. However, after the combination with Python, development efficiency greatly improved, although used to do very formal business software is not appropriate, but write small program is very convenient.

Now cut to the chase, Wxwidgets is an event-driven system that needs to be hooked up with event handlers for triggered events. In Python, this function is in the form of:

wx.Frame.Bind(self, event, handler, source=None, id=-1, id2=-1)

In general use, we basically only given event, handler and Source,event are the names of events, handler is the handler function, source is the event, such as a Button1 clicked and a Evt_button event occurred, If we use self. OnButton1 () to deal with, would write like this:

self.Bind(wx.EVT_BUTTON, self.OnButton1, self.Button1)

Here, Self is a frame (something like a form in Wxpython), and Button1 is a button,self placed under the frame. OnButton1 is the event handler function. And Wxpython is dead. The event handler function forms:

def handler(self,event):...

The event handler can only accept two parameters, one or self. As for the event, it is a matter of knowing what happened at the first sight. But then there's a problem: if I want to create a batch of buttons or menu keys (which I think is common) and want to use the same function to handle them, how does this function tell which button triggered the event? Obviously we want to pass some more parameters. QT is easy to implement, but wxwidgets is tricky.

I did not write the wxwidgets program in C + +--to be exact copy of a example, but I do not know whether it is my English is too torn or really not, I did not find the wxwidgets in the Guide to compile instructions, and finally can not compile it. So I don't know if this restriction is a C + +, but Python can solve this problem with lambda. In the following example, I use a menu.

First we build a Onmenusclick function:

def OnMenusClick(self, event, mark):....

This function receives more than one mark, and when bound, it is not to bind the Onmenusclick directly, but to pass a function that has been wrapped by lambda. The following example creates a bunch of menu keys and numbers them, and Onmenusclick can receive their numbers:

menu=wx.Menu()for i in range(0,N):btn=menu.Append(wx.NewId(), str(i))self.Bind(wx.EVT_MENU, lambda evt, mark=i : self.OnMenusClick(evt,mark) ,btn )

It's done! Does it feel a little magic? The point is, this lambda thing.

lambda evt, mark=i : self.OnMenusClick(evt,mark)

This sentence actually produces a function that receives only one parameter of EVT and passes it to self. Onmenusclick, so that you can achieve the purpose of passing more parameters to the event handler function.

def OnClick (EVT,AGRV):     Print AGRV     Print evt     def func (evt,function):    function (evt)    func (2,Lambda evt,argv=4:onclick (EVT,ARGV))

Passing parameters to the Wxpython event handler function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.