1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#!/usr/bin/python
import Tkinter
from Tkinter import *
import tkMessageBox
import tkFont
import sys
import traceback
#import Tix
from tkFileDialog import *
import os
import string
class ButtonsPanel(Frame):
def __init__(self, parent):
self.m_Parent = parent
Frame.__init__(self, parent)
# Define the 3 buttons
self.m_Button1 = Button(self)
self.m_Button2 = Button(self)
self.m_Button3 = Button(self)
# Grid the 3 buttons
self.m_Button1.grid()
self.m_Button2.grid(row=0,column=1)
self.m_Button3.grid(row=0,column=2)
# Bind Press and Release events on buttons
self.bind_all("<ButtonPress-1>", self.cmdPress)
self.bind_all("<ButtonRelease-1>", self.cmdRelease)
# Bind events when mouse enters into a button
self.bind_all("<Enter>", self.cmdEnter)
def cmdPress(self, event):
if self.winfo_containing(event.x_root, event.y_root) == self.m_Button1:
print "Button 1 pressed"
elif self.winfo_containing(event.x_root, event.y_root) == self.m_Button2:
print "Button 2 pressed"
elif self.winfo_containing(event.x_root, event.y_root) == self.m_Button3:
print "Button 3 pressed"
def cmdRelease(self, event):
if self.winfo_containing(event.x_root, event.y_root) == self.m_Button1:
print "Button 1 released"
elif self.winfo_containing(event.x_root, event.y_root) == self.m_Button2:
print "Button 2 released"
elif self.winfo_containing(event.x_root, event.y_root) == self.m_Button3:
print "Button 3 released"
def cmdEnter(self, event):
if self.winfo_containing(event.x_root, event.y_root) == self.m_Button1:
print "Entered in button 1"
elif self.winfo_containing(event.x_root, event.y_root) == self.m_Button2:
print "Entered in button 2"
elif self.winfo_containing(event.x_root, event.y_root) == self.m_Button3:
print "Entered in button 3"
if __name__ == "__main__":
# Instance of the main window application
app = Tk()
panel = ButtonsPanel(app)
panel.grid()
# Wait for events
app.mainloop() |
Partager