Tkinter event binding, how to transmit Parameters

Source: Internet
Author: User

Generally, event processing is as follows:

 
# Coding = utf-8import tkinterdef handler (): '''event processing function''' print "handler" If _ name __= = '_ main __': root = tkinter. TK () # Use the mediation function handleradapotor to set the command to BTN = tkinter. button (text = U' button ', command = handler) BTN. pack () root. mainloop ()

But what should I do if the handler () function requires parameters? It's very easy to use lambda.

 
# Coding = utf-8import tkinterdef handler (a, B, c): '''event handler ''' print "handler", A, B, CIF _ name __= = '_ main _': Root = tkinter. TK () # Use the mediation function handleradapotor to set the command to BTN = tkinter. button (text = u'button ', command = lambda: handler (a = 1, B = 2, c = 3) BTN. pack () root. mainloop ()

Lambda is really an artifact ~~

But what should I do if I want to use event like BTN. BIND ("<button-1>", Handler)? I 'd like to write an intermediate adapter function.

# Coding = utf-8import tkinterdef handler (event, a, B, c): '''event handler ''' print eventprint "handler", a, B, cdef handleradaptor (fun, ** kwds): ''' the adapter of the event processing function, which is equivalent to an intermediary. Where did the event come from, this may be the greatness of Python. '''return Lambda event, fun = fun, kwds = kwds: Fun (event, ** kwds) if _ name __= = '_ main _': Root = tkinter. TK () BTN = tkinter. button (text = U' button ') # bind the event to BTN using the mediation function handleradaptor. BIND ("<button-1>", handleradaptor (handler, A = 1, B = 2, c = 3) BTN. pack () root. mainloop ()

If you do not want to use a = 1, B = 2, c = 3 lexicographic parameters, you can modify the ** kwds parameter of handleradaptor () to * ARGs, which is flexible! **, * Indicates that anyone who wants to learn Python should know it!

Related Article

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.