Button event binding: without passing the event as an argument : Button « GUI Tk « Python

Python
1. 2D
2. Application
3. Buildin Function
4. Class
5. Data Structure
6. Data Type
7. Database
8. Development
9. Dictionary
10. Event
11. Exception
12. File
13. Function
14. GUI Pmw
15. GUI Tk
16. Language Basics
17. List
18. Math
19. Network
20. String
21. System
22. Thread
23. Tuple
24. Utility
25. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Python » GUI Tk » ButtonScreenshots 
Button event binding: without passing the event as an argument
Button event binding: without passing the event as an argument

from Tkinter import *

class MyApp:
  def __init__(self, parent):
    self.myParent = parent   
    self.myContainer1 = Frame(parent)
    self.myContainer1.pack()
    
    button_name = "OK"
    
    # command binding
    self.button1 = Button(self.myContainer1,
      command = lambda 
      arg1=button_name, arg2=1, arg3="Good stuff!" :
      self.buttonHandler(arg1, arg2, arg3)
      )
    
    # event binding -- passing the event as an argument
    self.button1.bind("<Return>"
      lambda 
      event, arg1=button_name, arg2=1, arg3="Good stuff!" 
      self.buttonHandler_a(event, arg1, arg2, arg3)
      )
         
    self.button1.configure(text=button_name, background="green")  
    self.button1.pack(side=LEFT)
    self.button1.focus_force()  # Put keyboard focus on button1    
    
    button_name = "Cancel"
    
    self.button2 = Button(self.myContainer1,
      command = lambda 
      arg1=button_name, arg2=2, arg3="Bad  stuff!"
      self.buttonHandler(arg1, arg2, arg3)
      )

    self.button2.bind("<Return>"
      lambda 
      event, arg1=button_name, arg2=2, arg3="Bad  stuff!" 
      self.buttonHandler(arg1, arg2, arg3)
      )
  
    self.button2.configure(text=button_name, background="red")
    self.button2.pack(side=LEFT)   
    
    
    
   def buttonHandler_a(self, event, argument1, argument2, argument3):
    print "buttonHandler_a received event", event
    self.buttonHandler(argument1, argument2, argument3)
    
root = Tk()
myapp = MyApp(root)
root.mainloop()
           
       
Related examples in the same category
1. Add a button to frameAdd a button to frame
2. Simple Button ActionSimple Button Action
3. Disable a buttonDisable a button
4. Enable a buttonEnable a button
5. Set Button to get focusSet Button to get focus
6. Button Border stylesButton Border styles
7. Button action to set Button backgroundButton action to set Button background
8. Button focusButton focus
9. create a button, and put it into a Containercreate a button, and put it into a Container
10. Set Button background by property nameSet Button background by property name
11. Specify the button's propertiesSpecify the button's properties
12. Button action inside a classButton action inside a class
13. Button widthButton width
14. Buttons on a toolbarButtons on a toolbar
15. Button foreground colorButton foreground color
16. Creating buttonsCreating buttons
17. Button single click and double clickButton single click and double click
18. Use lambda to generate a call back function for a buttonUse lambda to generate a call back function for a button
19. adds user-defined callbacks as button actionadds user-defined callbacks as button action
20. Subclasses buttonSubclasses button
21. Subclass HelloButton: redefine press-handler methodSubclass HelloButton: redefine press-handler method
22. Sub class button: add callback method and pack myselfSub class button: add callback method and pack myself
23. Sub class button: add callback method and use a dictionarySub class button: add callback method and use a dictionary
24. Button BorderButton Border
25. Button border size Button border size
26. Button border effects: RAISEDButton border effects: RAISED
27. Button background, foreground and fontButton background, foreground and font
28. Button width and heightButton width and height
29. Config button propertiesConfig button properties
30. Add gif image to a buttonAdd gif image to a button
31. Bind mouse single click and double click to a buttonBind mouse single click and double click to a button
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.