Print a description of an event, based on its attributes : Event « Tkinker « Python Tutorial

Home
Python Tutorial
1.Introduction
2.Data Type
3.Statement
4.Operator
5.String
6.Tuple
7.List
8.Dictionary
9.Collections
10.Function
11.Class
12.File
13.Buildin Function
14.Buildin Module
15.Database
16.Regular Expressions
17.Thread
18.Tkinker
19.wxPython
20.XML
21.Network
22.CGI Web
23.Windows
Python Tutorial » Tkinker » Event 
18.12.2.Print a description of an event, based on its attributes
Print a description of an event, based on its attributes
from Tkinter import *

class MyApp:
  def __init__(self, parent):
    self.myParent = parent   
    self.myContainer1 = Frame(parent)
    self.myContainer1.pack()
    
    self.button1 = Button(self.myContainer1)
    self.button1.configure(text="OK", background= "green")
    self.button1.pack(side=LEFT)
    self.button1.focus_force()       
    self.button1.bind("<Button-1>", self.button1Click)  
    self.button1.bind("<Return>", self.button1Click
    
    self.button2 = Button(self.myContainer1)
    self.button2.configure(text="Cancel", background="red")   
    self.button2.pack(side=RIGHT)
    self.button2.bind("<Button-1>", self.button2Click)   
    self.button2.bind("<Return>", self.button2Click
    
  def button1Click(self, event)
    report_event(event)       
    if self.button1["background"== "green":  
      self.button1["background""yellow"
    else:
      self.button1["background""green"
  
  def button2Click(self, event):
    report_event(event)
    self.myParent.destroy()      
  
def report_event(event):   
  event_name = {"2""KeyPress""4""ButtonPress"}
  print "Time:", str(event.time)
  print "EventType=" + str(event.type), \
    event_name[str(event.type)],\
    "EventWidgetId=" + str(event.widget), \
    "EventKeySymbol=" + str(event.keysym)
    
      
root = Tk()
myapp = MyApp(root)
root.mainloop()
18.12.Event
18.12.1.Display all attributes of an eventDisplay all attributes of an event
18.12.2.Print a description of an event, based on its attributesPrint a description of an event, based on its attributes
18.12.3.Variable length parameters for event methodVariable length parameters for event method
18.12.4.Demonstrates binding an event with an event handlerDemonstrates binding an event with an event handler
18.12.5.Call sys.exit command from ButtonCall sys.exit command from Button
18.12.6.Call root.quit from buttonCall root.quit from button
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.