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
| from Tkinter import *
class BoutonClik(Frame):
"""Definit un bouton cliquable"""
def __init__(self , boss = None, commande = None,larg = 0, haut = 5, couleur = 'yellow', bande = 3 , texte = ' Cliquez ici !', couleurmot = 'navy'):
Frame.__init__(self,boss)
self.haut,self.larg=haut,larg
# si seule une hauteur est rentree, le bouton sera carre
# sinon, on aura un rectangle
if self.larg == 0:
self.larg = 2*self.haut
self.lbl = Label(self, width = self.larg, height = self.haut ,bd = bande, text = texte, bg= couleur, relief = RAISED, fg= couleurmot)
self.lbl.pack()
self.lbl.bind("<Button-1>", self.action)
self.lbl.bind("<Button1-ButtonRelease>",self.retour)
self.bind("<Enter>",self.onEnter)
self.commande=commande
def action(self,event):
self.lbl.config(relief = SUNKEN)
def retour(self,event):
self.lbl.configure(relief = RAISED)
if self.commande:
self.commande()
def onEnter(self,event):
self.lbl.event_generate("<Button-1>") # génère un click sur le Label
def onclick():
print 'click'
if __name__== '__main__':
fen1=Tk()
fr=Frame(fen1,bg='brown', height =200, width = 200)
fr.pack()
fr2=Frame(fen1,bg='yellow', height =200, width = 200)
fr2.pack()
BoutonClik(boss=fr).pack(padx=5,pady=5)
BoutonClik(fr2, couleur = 'red', couleurmot = 'yellow', bande = 5, larg = 17, haut = 6,commande=onclick).pack(padx=5,pady=5)
fen1.mainloop() |