【发布时间】:2019-01-20 22:47:43
【问题描述】:
正如标题所暗示的那样...有没有办法在选中复选框时更改复选框上的文本颜色,然后在取消选中复选框时返回默认颜色?
【问题讨论】:
标签: python checkbox text tkinter colors
正如标题所暗示的那样...有没有办法在选中复选框时更改复选框上的文本颜色,然后在取消选中复选框时返回默认颜色?
【问题讨论】:
标签: python checkbox text tkinter colors
是的,当Checkbutton 开启时,您确实可以更改文本的颜色。要更改文本的颜色,请使用选项fg。我创建了两个变量off_color 和on_color。 Checkbutton关闭时,文字颜色为red按钮,打开时颜色为green。
这是一个代码:
from tkinter import *
import tkinter
root = Tk()
off_color = "red"
on_color = "green"
def on_check(): #this function will run on click on checkbutton
if cbVar.get() == "1":chbox["fg"] = on_color # if (get current checkbutton state) is "1" then....
else:chbox["fg"] = off_color
cbVar = StringVar(root) #making variable for checkbutton
cbVar.set(0) #turning off the checkbutton (initialy)
chbox = Checkbutton(root,variable = cbVar,text="Check me",command=on_check,fg=off_color) #making the checkbutton
chbox.place(x=0,y=0) #placing the checkbutton
mainloop()
【讨论】:
cb.Var.set(0) 关闭检查按钮。