faqts : Computers : Programming : Languages : Python : Tkinter

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

12 of 15 people (80%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I create a button that remains 'pushed' when pressed and is released when pressed again?

Aug 5th, 2000 22:21
unknown unknown, Richard Chamberlain


There are two ways to do this (well probably more than two ways :) )
from Tkinter import *
root=Tk()
myVar=IntVar()
checkbutton=Checkbutton(root,text="My
Button",indicatoron=0,varible=myVar)
checkbutton.pack()
def printme(event):
        print myVar.get()
button.bind('<Button>',printme)
root.mainloop()
This way uses a Checkbutton (you could also use a Radiobutton) and the
indicatoron option which makes it look like a button essentially.
The other way:
from Tkinter import *
root=Tk()
button=Button(root,text="My Button")
button.pack()
def changeme(event):
        if button.cget('relief')==SUNKEN:
                button.config(relief=RAISED)
        else:
                button.config(relief=SUNKEN)
button.bind('<Button>',changeme)
root.mainloop()
Which just captures a button event and changes the relief of the button
accordingly.