简体   繁体   English

tkinter 中按钮的两种不同的活动背景颜色

[英]two different activebackground color for a button in tkinter

Hello so I'm learning tkinter in python 3.10.5 and so take a look at this and I'll explain你好,所以我在 python 3.10.5 中学习 tkinter 等看看这个,我会解释

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.所以我有一个按钮可以占用整个 window 并且我想在 activebackground 部分中写下,如果按钮中没有文本,则 activebackground 颜色将是深红色,否则为石灰。 how to do that?怎么做?

Set activebackground within btn.config method and use if... else to toggle.btn.config方法中设置activebackground并使用if... else来切换。 Eg:例如:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM