Python events:
-----------------
-The program enters mainloop() which wait for events (user actions).
-We define the button which has a callback to the function callback().
-master is the root window, the window where your button will appear in.
- Events can be key presses or mouse operations by the user.
Note:
----------------------
I) Mouse Events:
----------------------
A). Button Events:
------------------
1) <Button-1> = left mouse button
2) <Button-2> = middle button
3) <Button-3> = The rightmost mouse button
4) <Button-4> = defines the scroll up event on mice with wheel support
5) <Button-5> = the scroll down.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
B). Motion Events:
------------------
1) <B1-Motion> = Left
2) <B2-Motion> = Middle
3) <B3-Motion> = Right
~~~~~~~~~~~~~~~~~~~~~~~~~~~
C). ButtonRelease Events:
--------------------------
1) <ButtonRelease-1> = Left Button is released
2) <ButtonRelease-2> = Middle Button is released
3) <ButtonRelease-3> = Right Button is released
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
D). DoubleButton Events:
--------------------------
1) <Double-Button-1> = Left
2) <Double-Button-2> = Middle
3) <Double-Button-3> = Right
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E). Mouse Entered & Leave Events:
----------------------------
1) <Enter> = Mouse Enter to widgets
2) <Leave> = Mouse Leave from widgets
-----------------------------------------------------------------------------
II) Keyboard Events:
----------------------
1) <FocusIn> - Focus to widget
2) <FocusOut> - Focus from widget
3) <Return> - User Press Enter Key
4) <Key> - User Pressed any Key
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 1 : tkbutton with onclick event
from tkinter import *
master = Tk()
def msg():
print ("click!")
b = Button(master, text="OK", command=msg)
b.pack()
mainloop()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 2 : tkbutton with bind method
from tkinter import *
master = Tk()
def msg(event):
print ("click!")
b = Button(master, text="OK")
master.bind('<Button-1>',msg)
b.pack()
mainloop()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 3 : tkbutton with onclick event
from tkinter import *
master = Tk()
master.minsize(300,100)
master.geometry("320x100")
def callback():
print ("click!")
photo=PhotoImage(file="1.gif")
b = Button(master,image=photo, command=callback, height=50, width=150)
b.pack()
mainloop()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 4 : tkbutton with onclick event
from tkinter import *
master=Tk()
master.minsize(300,120)
master.geometry("320x120")
def add():
a=int(a1.get())
b=int(a2.get())
c=a+b
a3.set(c)#print c
def sub():
a=int(a1.get())
b=int(a2.get())
c=a-b
a3.set(c)#print c
def clear():
a1.set("")
a2.set("")
a3.set("")
a1=StringVar() #a1=IntVar()
a2=StringVar() #a2=IntVar()
a3=StringVar() #a3=IntVar()
t1=Entry(master,textvariable=a1)
t1.focus_set()
t1.pack()
t2=Entry(master,textvariable=a2)
t2.pack()
b1=Button(master,text="Add",command=add,width=20)
b1.pack()
b2=Button(master,text="Sub",command=sub,width=20)
b2.pack()
b3=Button(master,text="Clear",command=clear,width=20)
b3.pack()
t3=Entry(master,textvariable=a3)
t3.pack()
master.mainloop()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 5 : tkbutton with bind method
from tkinter import *
master=Tk()
master.minsize(300,120)
master.geometry("320x220")
def add(event):
a=int(a1.get())
b=int(a2.get())
c=a+b
a3.set(c)#print c
def sub(event):
a=int(a1.get())
b=int(a2.get())
c=a-b
a3.set(c)#print c
def clear(event):
a1.set("")
a2.set("")
a3.set("")
a1=StringVar() #a1=IntVar()
a2=StringVar() #a2=IntVar()
a3=StringVar() #a3=IntVar()
t1=Entry(master,textvariable=a1)
t1.focus_set()
t1.pack()
t2=Entry(master,textvariable=a2)
t2.pack()
b1=Button(master,text="Add",width=20)
b1.bind('<Button-1>',add)
b1.pack()
b2=Button(master,text="Sub",width=20)
b2.bind('<Button-1>',sub)
b2.pack()
b3=Button(master,text="Clear",width=20)
b3.bind('<Button-1>',clear)
b3.pack()
t3=Entry(master,textvariable=a3)
t3.pack()
master.mainloop()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 6: Button single click & double click
from tkinter import *
import sys
master=Tk()
def hello(event):
print("Single Click, Button-l")
def quit(event):
print("Double Click, so let's stop")
sys.exit()
master.withdraw()
widget = Button(master, text='Mouse Clicks')
widget.pack()
widget.bind('<Button-1>', hello)
widget.bind('<Double-1>', quit)
master.mainloop()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 7: Mouse events
from tkinter import *
class MouseLocation( Frame ):
def __init__( self ):
Frame.__init__( self )
self.pack( expand = YES, fill = BOTH )
self.master.title( "Demonstrating Mouse Events" )
self.master.geometry( "275x100" )
self.mousePosition = StringVar() # displays mouse position
self.mousePosition.set( "Mouse outside window" )
self.positionLabel = Label( self,textvariable = self.mousePosition )
self.positionLabel.pack( side = BOTTOM )
self.bind( "<Button-1>", self.buttonPressed )
self.bind( "<ButtonRelease-1>", self.buttonReleased )
self.bind( "<Enter>", self.enteredWindow )
self.bind( "<Leave>", self.exitedWindow )
self.bind( "<B1-Motion>", self.mouseDragged )
def buttonPressed( self, event ):
self.mousePosition.set( "Pressed at [ " + str( event.x ) + ", " + str( event.y ) + " ]" )
def buttonReleased( self, event ):
self.mousePosition.set( "Released at [ " + str( event.x ) + ", " + str( event.y ) + " ]" )
def enteredWindow( self, event ):
self.mousePosition.set( "Mouse in window" )
def exitedWindow( self, event ):
self.mousePosition.set( "Mouse outside window" )
def mouseDragged( self, event ):
self.mousePosition.set( "Dragged at [ " + str( event.x ) + ", " + str( event.y ) + " ]" )
MouseLocation().mainloop()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
No comments:
Post a Comment