G
G
GLEK THE GREAT2021-07-06 13:49:24
Python
GLEK THE GREAT, 2021-07-06 13:49:24

How to make a tkinter button stay pressed until another is pressed?

How to click on a button once and keep it pressed until another button is clicked?
Here is the code:

root = tkinter.Tk()
root.geometry('250x350')
root.configure(bg = "white")
root.title("")
root.resizable(False, False)

lb = tkinter.Label(root, text = text)
lb.configure(bg = "white")
lb.place(x = 25, y = 25, height=25, width=200)

but1 = tkinter.Button(root, text = "Привет!", command=main)
but1.config(bd = 1, font = ("Castellar", 25), bg = "white")
but1.place(x = 50, y = 160, height = 50, width = 150)

but2 = tkinter.Button(root, text = "Пока!", command=bye)
but2.config(bd = 1, font = ("Castellar", 25), bg = "white")
but2.place(x = 50, y = 220, height = 50, width = 150)

root.mainloop()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kozhevin, 2021-07-06
@glek1

You need the relief button property - SUNKEN and RAISED
Shitcode:

def change_but1():
    but2['bg'] = '#ffffff'
    but2['relief'] = tkinter.RAISED    
    but1['bg'] = '#eeeeee'
    but1['relief'] = tkinter.SUNKEN
    
    
def change_but2():
    but1['bg'] = '#ffffff'
    but1['relief'] = tkinter.RAISED    
    but2['bg'] = '#eeeeee'
    but2['relief'] = tkinter.SUNKEN


but1 = tkinter.Button(root, text = "Привет!", command=change_but1)
but1.config(bd = 1, font = ("Castellar", 25), bg = "white")
but1.place(x = 50, y = 160, height = 50, width = 150)

but2 = tkinter.Button(root, text = "Пока!", command=change_but2)
but2.config(bd = 1, font = ("Castellar", 25), bg = "white")
but2.place(x = 50, y = 220, height = 50, width = 150)

D
Daniil Shevkunov, 2021-07-06
@danila763

you have buttons attached to the main and bye functions, start these functions like this:

def main():
    but1['relief'] = 'sunken'
    but2['relief'] = 'raised'

def bye():
    but1['relief'] = 'raised'
    but2['relief'] = 'sunken'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question