Home > Software design >  two different activebackground color for a button in tkinter
two different activebackground color for a button in tkinter

Time:08-08

Hello so I'm learning tkinter in python 3.10.5 and so take a look at this and I'll explain

from tkinter import*
window=Tk()
window.geometry('400x400')
window.resizable(width=False,height=False)
def txt():
    btn.config(text='test')
btn=Button(window,text='',activebackground=('crimson' if btn else 'lime'),command=txt)
btn.pack(expand=True,fill=BOTH)

window.mainloop()

so I have a button that takes the whole window and I want to write in activebackground part that if the button has no text in it activebackground color would be crimson otherwise lime. how to do that?

CodePudding user response:

Set activebackground within btn.config method and use if ... else to toggle. E.g.:

from tkinter import*
window=Tk()
window.geometry('400x400')
window.resizable(width=False,height=False)

def txt():
    if btn['text'] == '':
        btn.config(text='test', activebackground='lime')
    else:
        btn.config(text='', activebackground='crimson')
    
btn=Button(window,text='',activebackground='crimson', command=txt)
btn.pack(expand=True,fill=BOTH)

window.mainloop()
  • Related